1

I have documents like this in Mongoose (MongoDb):

{
  begin: Date,
  end: Date
}

All I'd like to do is to:

  1. Select all documents with end == null
  2. and then update them using end = "begin increased by 10 days"

How can it be done within just a single update?

Cartesius00
  • 23,584
  • 43
  • 124
  • 195

2 Answers2

1

This query selects all documents with end = null and update it with the new date Collection.update({end:null}, {$set: {end:'new_date'}},{ multi: true });

Islam Ahmed
  • 539
  • 1
  • 5
  • 17
1

Right now, you cannot reference a document's current properties in an update(). Instead, you'll have to iterate through the documents as described in this answer. In your case, it will look more like this:

db.docs.find({end:null}).forEach(
  function(doc) {
    doc.end = doc.begin + 10;
    db.docs.save(doc);
  }
)

This is the syntax for the Mongo shell-- you might have to make some changes for Mongoose, as per the API.

Community
  • 1
  • 1
Amalia
  • 896
  • 7
  • 18