-1

I have do do something like this (please forgive me about the stupidity of the example... :-):

var dateRef = new Date(1990, 1, 1);
Person
  .where('dateOfBirth').gte(dateRef)
  .update({ $set: { isYoung: true } }, function (err, count) {
  })
;

I.e.: I'd like to $set to true field isYoung for every Person document whose date of birth (dateOfBirth) is later than a reference date.

My code is working for just one person, even if (currently) many documents do match the condition (all young people, here... :-).

Any clue?


P.S.: as a bonus, I'd like to set not-matching persons isYoung field to false, too, if possible...

MarcoS
  • 17,323
  • 24
  • 96
  • 174

1 Answers1

1

You should use multi:true in update to update multiple documents like :

var dateRef = new Date(1990, 1, 1);
Person
  .where('dateOfBirth').gte(dateRef)
  .update({ $set: { isYoung: true } },{multi: true}, function (err, count) {
  });
Vishwas
  • 6,967
  • 5
  • 42
  • 69
  • Didn't work for me. Call Person.update(...) see http://stackoverflow.com/questions/33876655/update-of-multiple-documents-with-mongoose-fails/33879548#33879548 – Matthias M Nov 23 '15 at 20:05