I'm working with mongo 3.4.13 and I want to add an element in this document:
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A0001",
"subItems": [
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A1001",
"subItems": []
},
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A2001",
"subItems": [
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A2101",
"subItems": [
//I wanna add an element here
]
}
]
}
]
}
But I have no idea how to do it, any idea?
According to description as mentioned into above question I assume that you need to add an element into subItems array field belonging to BSON Document as mentioned above.
$addToSet operator is used to append a value into an array field as a part of update operation only if value does not exist into an array thereby ensuring uniqueness of values into an array field.
Related
I have a Mongo database with multiple documents and they all contain 2 Items for in store and online locations. The difference will be that attributes within the items object may differ. I am trying to capture all documents that have differing attributes between the object items with the array.
I've tried using $expr and comparing the position of the elements such as "Items.0.Attributes" == "Items.1.Attributes" but had no luck.
{
"ID" : "123456789",
"Items" : [
{
"ItemDept" : "softLines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts"
},
"Attributes" : [ "blue","small","mens"],
"Season" : "Summer"
"Location":"online"
}
,
{
"ItemDept" : "softlines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts")
},
"Attributes" : [ "blue","small","women"],
"Season" : "Summer"
"Location":"stores"
}
]
}
If I understand what you want to find/output, here's one way you could do it.
db.collection.find({
"$expr": {
"$not": {
"$setEquals": [
{"$first": "$Items.Attributes"},
{"$last": "$Items.Attributes"}
]
}
}
})
Try it on mongoplayground.net.
How do I query a deeply nested docs like show in the below image.
Here columns is an array of unknown size. Each element in the column contains a record that is again an array. Each element of the record array contains an array called fields. Each entry in the field contains 2 keys called name and value.
I'm querying the name of the innermost array (in fields array). I couldn't go above level 1 order of nesting.
JSON doc of the above image
"data" : {
"columns" : [
{
"name" : "styleId",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257752"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "styleId"
}
],
"rules" : [
[
]
]
}
]
},
{
"name" : "vendorArticleNumber",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257753"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "vendorArticleNumber"
}
],
"rules" : [
[
]
]
}
]
},
{
"name" : "vendorArticleName",
"record" : [
{
"fname" : "column_mapping",
"_id" : ObjectId("5ba488c79dc6d62c90257754"),
"fields" : [
{
"name" : "column_mapping_form",
"value" : "vendorArticleName"
}
],
"rules" : [
[
]
]
}
]
}
}
What can be the solutions if such kind of heaving nesting is there?
db.collection.find("data.columns.record.fields.name" : "column_mapping_form")
will match all documents where there is at least one element of columns has at least one record with at least one field where name is "column_mapping_form".
https://docs.mongodb.com/manual/tutorial/query-array-of-documents/ has very good explanation, examples, and interactive shell to play with.
I have collection as below
"_id" : "PS8720_18",
"AlertID" : "18",
"Status" : "ACTIVE",
"triggerHistory" : [
{"triggerId" : "1535081507421"},
{"triggerId" : "1535105196735"},
{"triggerId" : "1535341330335"},
{"triggerId" : "1535364578821"}
]
I want to delete all element and just want keep last two entries in the array. Each document has different no of elements in array. How do I achieve this?
Please Check with the following Query
db.getCollection('youtablename').update({}, {
$push: {
triggerHistory: {
$each: [ ],
$slice: -2
}
}},{multi:true})
Hope it Helps !!
Hi I'm trying to remove multiple objects from an array that looks like this.
{
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b3"),
"owner" : "1",
"group_name" : "PAASCU Board",
"group_members" : [
{
"faculty_name" : "Cheska Dela Rosa",
"faculty_number" : 2,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b5")
},
{
"faculty_name" : "Earl Sempio",
"faculty_number" : 7323,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b4")
},
{
"faculty_number" : 203,
"faculty_name" : "Sample",
"_id" : ObjectId("5a7dbf7952bd150a94d83958")
},
{
"faculty_number" : 8025,
"faculty_name" : "Sample Postman",
"_id" : ObjectId("5a7dc64a1cf5dd3d50167d53")
}
],
"__v" : 0 }
It works when I remove a single object using the $pull with this code.
db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
But what if I want to remove multiple objects with different faculty_number? I tried using the $each method just like how I add multiple objects in the array but it doesn't work well.
Use $in operator to pass the list of faculty values to remove documents from embedded array. More here
Try
db.groups.update(
{"_id": ObjectId("5a7da1bda21d5f3e8cf005b3")},
{"$pull":{"group_members":{"faculty_number":{$in:[8025,7323]}}}}
)
I have a requirement to update an array of multiple elements.My collections is in the following way
{
"_id" : ObjectId("53e87e239ae974e6a0a81004"),
"name" : "mulagala",
"notifications" : [
{
"name" : "apple",
"status" : 0
},
{
"name" : "microsoft",
"status" : 0
},
{
"name" : "android",
"status" : 0
}
]
}
now i want to change the every status element of the array should be changed to 1, ie.status:1 with a single query.
I tried in the following way
db.mystatus.update({'notifications.status':0},{$set:{'notifications.$.status':1}},false,true)
But the first record only updating, what to do.Any help would be appriciated!
Have you tried updating the elements of the array using the $ operator for arrays? Currently it updates only one element as the index is coded to 0.