Search code examples
node.jsmongodbmongodb-update

Mongodb Dot Notation Not Updating Array


I'm having a problem with mongodb's dot notation for arrays.

The 'bases' array in the user object has a set of bases in it which I'm trying to update via dot notation. To further complicate matters, I'm pushing the updates into an array of functions and calling them using the Async library.

//NOTE: baseIndex, baseData, dbCollection and baseOwner are considered defined already.
let updateArray = []
let thisUpdateObject = {}
thisUpdateObject['$set'] = {}
thisUpdateObject['$set']['bases.'+baseIndex] = baseData
let thisUpdate = function(callback){
    dbCollection.update({'id':baseOwner},thisUpdateObject,function(err, result){
        if (err){
            callback(err)
        } else {
            callback(null, result);
        }
    });
}
updateArray.push(thisUpdate)
async.parallel(updateArray,function(err, results){
            if...

My result says the following:

n: 0,
nModified: 0,
ok: 1 }

I've verified that the bases array exists inside the document that matches 'id':baseOwner. Element zero does exist in the array (which shouldn't matter, but it does already exist.)

When I paste my update to console, I get this:

'$set': { 'bases.0': { *contains correct object*}}

Finally, I know that the async update system is working because in cases where dot notation is not included, the update IS correctly changing documents.

I'm assuming something's wrong with my dot notation, but I can't see what.

Am I making any obvious errors?


Solution

  • A brief discussion in the comments yielded a simple answer:

    The n: 0 in the result indicates that no documents matched the update query. The culprit was an incorrect id value contained in the baseOwner variable.