This is my final document structure. I am on mongo 3.0.
{
_id: "1",
stockA: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ],
stockB: [ { size: "S", qty: 27 }, { size: "M", qty: 58 } ]
}
I am randomly adding elements inside stockA
or stockB
collections.
I would like come up with a query which would satisfy following rules:
- If
item
is not exist - create whole document and insert item instock A or B
collection. - If
item
is exist butstock A or B
collection is missing createstock
collection and addelement
inside collection. - if
item
is exist andstock
collection is exist just appendelement
inside collection.
Question: Is it possible to achieve all that in one query? My main requirement that current insert has to be extremely fast and scalable.
If yes, could you please help me to come up with required query.
If no, could you please guide me how do I achieve requirements in fastest and cleanest way.
Thanks for any help!