0

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?

6
  • 1
    Notice the n: 0 in your update result. That's telling you that your query isn't matching any documents. Ensure that baseOwner is of the expected value and type, and ensure that your use of id is correct and that you don't mean _id. Commented Sep 25, 2019 at 1:39
  • That was exactly the answer. ...Somehow my baseOwner got converted to a string instead of an int and it wasn't matching. THANK YOU! (I've been staring at this too long.) Commented Sep 25, 2019 at 1:43
  • Haha, no problem. It's easy to get tunnel vision like that. Glad you found the answer you were looking for! I'll go ahead and submit what I mentioned here as an answer so we can mark this as solved :) Commented Sep 25, 2019 at 1:49
  • Please do. Appreciate the help. Commented Sep 25, 2019 at 1:49
  • For the record, if the value from baseOwner came 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! Commented Sep 25, 2019 at 1:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.