mongodb php finding a subobject and removing it, $inc other subobject
Posted
by
Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-12-24T17:39:59Z
Indexed on
2011/01/02
17:54 UTC
Read the original article
Hit count: 321
I'm trying to delete the subobject 'apples' from my documents and update the 'fruitInventory' property, decrease by the amount of apples.
I'm confused on how to proceed, should I use dot notation or do a full text search for apples? I don't know if this matters but you can assume apples will always be in field 1.
// Document 1
{
"1": {
"apples": 3,
"fruitInventory": 21,
"oranges": 12,
"kiwis": 3,
"lemons": 3
},
"2": {
"bananas": 4,
"fruitInventory": 12,
"oranges": 8,
},
"_id": "1"
}
// Document 2
{
"1": {
"apples": 5,
"fruitInventory": 10,
"oranges": 2,
"pears": 3
},
"2": {
"bananas": 4,
"fruitInventory": 6,
"cherries": 2,
},
"_id": "2"
}
Result should be like this:
// Document 1
{
"1": {
"fruitInventory": 18,
"oranges": 12,
"kiwis": 3,
"lemons": "3"
},
"2": {
"bananas": 4,
"fruitInventory": 12,
"oranges": 8,
},
"_id": "1"
}
// Document 2
{
"1": {
"fruitInventory": 5,
"oranges": "2",
"pears": "3"
},
"2": {
"bananas": 4,
"fruitInventory": 6,
"cherries": 2,
},
"_id": "2"
}
Thanks in advance for your help.
© Stack Overflow or respective owner