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?
n: 0in your update result. That's telling you that your query isn't matching any documents. Ensure thatbaseOwneris of the expected value and type, and ensure that your use ofidis correct and that you don't mean_id.baseOwnercame from a form input from a webpage, then it's often the case that the default type for inputs is text. It's very common to expect a numeric value and actually receive a string in those cases. Tricky little bug right there!