it is required to process a data sample, and then make actions on one of the records in the sample

how to do it right?

via promise , callback or setTimeout

 model.update( { groupId : groupId }, { field1 : val1, field2 : val2 } ) 

UPDATE

I tried several options, but either the second update works ahead of time

c promise

 mongoose.Promise = global.Promise; model.update({ groupId: 5, order: {$gte: 0, $lt: 3} }, { $inc: { order: 1 } }, { multi: true }) .then(function(){ model.update({ id: 7 },{ order: 0 }); }); 

or with `callback '

 model.update({ groupId: 5, order: {$gte: 0, $lt: 3} }, { $inc: { order: 1 } }, { multi: true }, function(er, o){ err && console.error(err); model.update({ id: 7 },{ order: 0 }); }); 

or changes do not occur at all

 mongoose.Promise = global.Promise; model.find({ groupId: 5, order: {$gte: 0, $lt: 3} }) .exec() .then(function(ar){ for(var i = 0; i < ar.length; i++) { ar[i].order++; } return ar; }) .then(function(){ model.update({ id: 7 },{ order: 0 }); }); 

tell me what was done wrong?

  • in the latter case, return ar.save(); most likely - vp_arth
  • @vp_arth, in the promise version, we cannot use update instead of find ? or is there also a restriction due to thenables ? - ravend
  • The only difference with find is that in order to get Promise you need to call exec on thennables. When replacing find with update, this exec must be removed. - vp_arth

2 answers 2

setTimeout always there , it has nothing to do with a query in the database, so you never have any guarantees that you will wait for the end of the query.

But promises or callbacks are a matter of taste.

Nested callback functions often turn into hell.

Promises are more modern and allow you to make the code more readable.
It is important to remember that requests ( find ) in Mongoose return, there so-called. thenables , not promise . To get promise you need to call the .exec() method on the result.
Also, the documentation shows how easy it is to connect native Promise instead of embedded mpromise :

 // Use native promises mongoose.Promise = global.Promise; 

But they both allow you to complete your task.


Example:

 mongoose.Promise = global.Promise; Users.find({group_id: 5}).exec() .then(users => { // обработка пользователей return users[0]; }) .then(user => Users.update({id: user.id}, {$inc: {counter: 1}})) .then(() => console.log('finished')) .catch(err => console.error(err)); 

To update the question:

 var p = model.find({ groupId: 5, order: {$gte: 0, $lt: 3} }).exec(); p.then(function(ar){ for(var i = 0; i < ar.length; i++) { ar[i].order++; ar[i].save(); } return ar; }).then(function(){ return model.update( {id: 7}, {$set: {order: 0}} ); }).then(function(){ console.log('finished'); }).catch(function(err){ console.log('error: ', err); });; 
  • I understand the theory, but do not share the reference to the working example? - ravend
  • unfortunately we failed to consistently perform 2 update , updated the question - ravend

 model.update( { groupId: 5, order: { $gte: 0, $lt: 3 } }, { $inc: { order: 1 } }, { multi: true }, function(err, o) { if (err) { console.log(err); } else { model.update({id: 7}, {order: 0}, function(err, res) { console.log('done'); }); } } );