Hey i'm trying to update a nested Object in my MongoDB.
They structure is
[
{
"_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": "[email protected]",
"menuname": "MenünameX"
}
]
}
]
The nested ID in menus is another ID of a MongoDB document which is created dynamicly.
I try to update the menuname, but cant access it, since mongo always returns null or doesnt find the element since in need to search for only the key not the key - value pair.
What i 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" }});
So thanks to suzo i figgured out the solution. The MongoShell code that works:
UPDATE: Since my field is Mixed type, this isn't updating without getting marked!. So here is the complete correct code: