MongoDB, remove nested doc in an array - mongodb

I have the following structure in MongoDB and I try to remove the documents that contains specific tags. I can't seem to be able to get the $pull work.
In this example, I would like to pull the nested doc that has has tags :["BB"]
Any help will be appreciated !
{
"_id": 123,
"socialItems": {
"facebook": [{
"name": "firstFacebook",
"id": 2
}, {
"name": "secondFB",
"id": 43
}],
"instagram": [{
"name": "firstNstagram",
"id": 4
}],
"pc": [{
"name": "firstPC",
"id": 55,
"tags": [
"ab"
]
}, {
"name": "secondPC",
"id": 66,
"tags": [
"BB"
]
}]
}
}

I assume you are trying to drop the nested 'pc' doc, from the array? You also don't mention if you're using a specific driver for this, so I've assumed you're running this in the Mongo shell.
The following will remove documents from the 'pc' property, when containing the 'BB' tag.
db.collectionName.update({'socialItems.pc.tags': 'BB'}, {$pull: {'socialItems.pc': {tags: 'BB'}}})

Related

MongoDB, how to query nested Document/object/field in tree like structure

I have following document structure stored in Mongodb. Please note "next" is an array.
{
"id": 1,
"pricing": [
{
"name": "name1",
"value": "v1",
"next": [
{
"name": "name2",
"value": "v2",
"next": [
{
"name": "name3",
"value": "v3",
"next": [
]
}
]
}
]
}
]
}
How can I find the eg. name=name3 or any arbitrary value from it? Not knowing if it exists or not.
You can match the field pricing.next.next.name and the value name3 like this.
db.collection.find({
"pricing.next.next.name": "name3"
})
So it will return the document that contains the value name3 inside the two arrays.
Mongo Playground example here
Your question is not explained too well, so, maybe you mean, how to get only the field name after querying by name. Note that you can use other field like value to query the document.
Then, you have to do this:
db.collection.find({
"pricing.next.next.name": "name3",
},
{
"pricing.next.next.name": 1
})
And mongo Playground example here

Update items in a property with array type on MongoDB

I have a collection with documents like:
{
"_id": "Mongo ObjectID",
"some_prop": "some_value",
"features": [
{ "name": "A", "icon": "01.png" },
{ "name": "B", "icon": "02.png" }
]
}
Another document sample:
{
"_id": "Mongo ObjectID",
"some_prop": "other one",
"features": [
{ "name": "B", "icon": "02.png" },
{ "name": "C", "icon": "03.png" },
{ "name": "D", "icon": "04.png" }
]
}
Notice that in the first document and the second there is the same feature B. This occurs all over many documents.
What I need is to update all features B to a new icon, something like this:
{ "name": "B", "icon": "10.png" }
I need to apply this change for all documents that has a feature with name B.
I already did a very horrible code to get all documents and update one by one in a loop. But my guess is there is a better way to do it, maybe in a single collection.update command? I'm new in MongoDB and so far googling didnt work.
You need to use $positional operator to update the fields inside an array
db.collection.updateMany(
{ "features.name": "B" },
{ "$set": { "features.$.icon": "10.png" }}
)

MongoDB query for Find 2 levels object element

I have a big issue, i don't know what to do...
What I wanna is to find all objects with Object2 name. I have Object 2 with name element.
What I wanna is to find all objects with the value X in the element name inside Object2. in the example is the value name is ="IWANTALLOBJECTSWITHTHISNAME"
the Json structure.
"objects": [
{
"_id": "5c69a62cf9acf00d00dbc02d",
"date": "2222-02-24T00:00:00.000Z",
"description": "22",
"Object1": {
"_id": "5c69a62cf9acf00d00dbc02b",
"date": "2222-02-24T00:00:00.000Z",
"user": "5c30fd5890bbd24a1c46c7ee",
"positionsObject1": [
{
"id": 1,
"Object2": {
"_id":"5c69a62cf9acf00d00dbc02c",
"name": "IWANTALLOBJECTSWITHTHISNAME"
},
"description": "22",
"value": 22
}
],
"id": 13,
"__v": 0
},
"user": "5c30fd5890bbd24a1c46c7ee",
"id": 7,
"__v": 0
}
]
I'm new in mongoDB and this query is really really hard. I tried everything. Thank very much for the help.
You can specify the path using dot notation:
db.col.find({ "objects.Object1.positionsObject1.Object2.name": "IWANTALLOBJECTSWITHTHISNAME" })

What is the element query in Mongodb to get value?

I am working on mongodb Aggregation query but i don't know how to get the array value from the JSON by joining two collection, please go through my below sample JSON.
COKE Collection
[{
"name": "Mirinda",
"_id": "894567894769476948"
}, {
"name": "COKE",
"_id": "857856856879694769"
}, {
"name": "PEPSI",
"_id": "785485686846684684"
}]
COKE Order Collection
{
"_id": "0908080808031338013101",
"drink": [{
"name": "Mirinda",
"_id": "894567894769476948"
}, {
"name": "COKE",
"_id": "857856856879694769"
}, {
"name": "PEPSI",
"_id": "785485686846684684"
}]
}
Expected OutPut:
Miranda name and _id should come of Orders collection by passing _id.
Above sample son i want to get the name value by joining the collections _id, please kindly go through my post and let me know if you have any suggestion.

Find specific mongoldb document from nested array

This is my document in MongoDB:
{
"_id": {
"$oid": "566193b0c9b5290f234242"
},
"name": "fake-name-1",
"profiles": [
{
"real-name": "fake-name-1",
"color": "fake-color-1"
},
{
"real-name": "fake-name-2",
"color": "fake-color-2",
"active": true
},
{
"real-name": "fake-name-3",
"color": "fake-color-3"
}
]
}
I'm real newbie to MondoDb, and are trying to find the document where profiles contains a real-name with "MArtin43221" and active = true.
How do I create a search query for this?
I've tried:
{"profiles": ["real-name":"MArtin43221", "active":true]}
Try elemMatch:
db.collection.find({"profiles": {$elemMatch:{"real-name":"MArtin43221", "active":true}}})