0

I want to update all the database :-

Their are Models like:

{
    "_id" : ObjectId("593800da81dd4240d42f0c98"),
    "leaveType" : ObjectId("5927da8b7094b726fecaa3b5"),
    "units" : 20,
    "employee" : ObjectId("59283f772c3bd82eaca28153"),
    "__v" : 0
}

What I want is, update all the models with inserting new value as unitsAvailed with making same Number as Corresponding units

{
    "_id" : ObjectId("593800da81dd4240d42f0c98"),
    "leaveType" : ObjectId("5927da8b7094b726fecaa3b5"),
    "units" : 20,
    "unitsAvailed" : 20,
    "employee" : ObjectId("59283f772c3bd82eaca28153"),
    "__v" : 0
}
hardy
  • 880
  • 2
  • 13
  • 30

3 Answers3

0

you can use this query to update all documents

var cursor = db.collectionName.find({});
cursor.forEach(function(data) {
  data.unitsAvailed = data.units;
  data.save();
});
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
0

You can run server-side code with db.eval().see below example

 db.eval(function() { 
  db.collection.find({tag : "refurb"}).forEach(function(e) {
    e.Price = e.Price * 0.5;
    db.collection.save(e);
  });
});

see Multiply field by value in Mongodb

Nitu Bansal
  • 3,826
  • 3
  • 18
  • 24
-1

You can use this:

 db.collectionName.updateMany({},{$set:{unitsAvailed: 20}})
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47