JavaSheriff
JavaSheriff

Reputation: 7687

MongoDb updating existing documents with sequence/unique id?

I would like to update existing documents with a unique id

When I execute the following function,
I am not sure what is right scope for the internal counter?

db.myDocs.find().forEach(function(doc){
    db.myDocs.update({_id :  doc._id}, {$set: {
        unique_id : COUNTER_HERE?
    }});     
   COUNTER++;
});

One solution will be to create additional count collection but I would like to avoid that if possible.

EDIT

unique id per this for loop only.

Upvotes: 0

Views: 327

Answers (1)

Osman Goni Nahid
Osman Goni Nahid

Reputation: 1279

You have to use {new : true} if you add value into new property in doc and {multi: true} for multiple documents update as the option.

Upvotes: 1

Related Questions