I have a collection of documents for the form:
{ name:String, groceries:{ apples:Number, cherries:Number, prunes:Number } }
Now, every query I have to increment with positive and/or negative values for each element in "groceries". It is not important what keys or how many, I just added some examples.
I could do a :
var dataToBeIncremented = stuff;
var $inc = {};
for each( var index in dataToBeIncremented )
{
$inc[ "groceries." + index ] = dataToBeIncremented[ index ];
}
then
db.update( { _id:targetID }, { $inc : query } )
however, I might have thousands of grocery elements and find doing this loop at each update to be ugly and unoptimized.
I would like to know how to void this or why it can't be optimized.