Thursday 18 May 2017

why doesn't Mongoose ID as parameter work in update operation?

Look at the following code, which is pretty straight and works fine

modelName.update({'_id':mongoose.Types.ObjectId("59198a3e73ae303f97e97e91")},
    {$inc:{'counter':1}})

And I get expected output

{ ok: 1, nModified: 1, n: 1 }

But if I write exactly same code wrapped inside a function and pass mongoose object Id as an argument, my value suddenly stops incrementing. Look at following example code

function incCounter(objID){
    modelName.update({'_id':mongoose.Types.ObjectId(objID)},
    {$inc:{'counter':1}})
}

incCounter("59198a3e73ae303f97e97e91")

Now my output strangely is

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

I'm pretty confused that why the same query is producing different output inside and outside function



via Point Networks

No comments:

Post a Comment