Firdous Firdous - 1 year ago 78
Node.js Question

Simple mongo object upsert using strongloop

I am trying to do a simple upsert operation on a particular record where the field to be updated is a number type.
I fetch the record using its record id, just +1 the number field and save it again, but for some reason it wont increment/update as seen from the log

The code is:

customer.findById(, function(err, objectResult){


objectResult.imageIndex = 1;
var index = objectResult.imageIndex;
objectResult.imageIndex = index++;

customer.upsert(objectResult, function(err, response){});

Both the console.log would display same value.. why wont it increase and update, any thing wrong with the code?

Answer Source

The issue is with the statement:

objectResult.imageIndex = index++;

This is because index++ does the assignment first and then increments the value of index.

It should be either:

objectResult.imageIndex = ++index;


objectResult.imageIndex = index + 1;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download