runtimeZero
runtimeZero

Reputation: 28126

Mongo findAndUpdate for updating multiple documents

I am attempting to use mongo findAndUpdate to update multiple documents and keep receiving syntax error:

db.forecasts.findAndModify({
   query: {forDate: ISODate("2016-02-25T05:00:00.000+0000")},
   update: {
      { $set: {forDate: ISODate("2016-02-23T05:00:00.000+0000")}}, 
      {multi: true}
     }
   })

Alternately I also tried:

db.forecasts.update({
   {'forDate': ISODate("2016-02-25T05:00:00.000+0000")},
   {'forDate': ISODate("2016-02-23T05:00:00.000+0000")}, 
   {multi: true}  
   })

The error I receive is : Error at line 2 position 3: <missing ')'

Upvotes: 5

Views: 13680

Answers (3)

Nishant
Nishant

Reputation: 3704

Use simple update query here, last 2 true's are for "upsert" and "multi"

db.forecasts.update( {"forDate":ISODate("2016-02-25T03:34:54Z")}, { $set : { "forDate" : new ISODate("2016-03-23T03:34:54Z") } }, true, true);

Upvotes: 5

sqa9999
sqa9999

Reputation: 31

findAndModify() doesn't allow {multi: true}. You can only update one document and it returns the original doc (default) or uou can ask for thr updated doc.

Upvotes: 4

runtimeZero
runtimeZero

Reputation: 28126

The following thread helped resolve the issue:

How to update date field in mongo console?

db.forecasts.update( {}, { $set : { "forDate" : new ISODate("2016-02-23T03:34:54Z") } }, true, true);

Upvotes: 0

Related Questions