Hey there, I'm currently working on updating a nested Object in my MongoDB.
The current structure looks like this:
[
{
"_id": "5871010d1ff9831574e7178d",
"created_at": "2017-01-07T14:54:05.791Z",
"updated_at": "2017-01-07T14:54:05.791Z",
"place_id": "ChIJ1eTO6eS1vkcRL_mX2RWUKo4",
"name": "Kunstwerk Restaurant Gummersbach",
"__v": 0,
"cooks": [],
"menus": [
{
"58727068ba8d6c04a0ea331f": [
{
"intolerances": {
"intolerance" : "false"
...
},
"nutritiondata" : [
{...}
],
"cookname": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c28392f2837333f341c28392f28723839">[email protected]</a>",
"menuname": "MenünameX"
}
]
}
]
The nested ID in the menus section is actually another ID of a MongoDB document that is generated dynamically.
I have been trying to update the "menuname" field, but I'm facing issues as MongoDB either returns null or doesn't find the element due to the need to search for only the key and not the key-value pair.
Here's what I have tested so far:
db.restaurants.update({ 'menus.58727068ba8d6c04a0ea331f', { $set : {"menus.$.menuname" : "test"}}});
db.restaurants.findOne({ 'menus.58727068ba8d6c04a0ea331f' : "58727068ba8d6c04a0ea331f"});
db.restaurants.find({ menus : { $elemMatch : {"58727068ba8d6c04a0ea331f": "58727068ba8d6c04a0ea331f" }}});
db.restaurants.findOne({ menus : { $eq : "58727068ba8d6c04a0ea331f" }});