Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mongo Update Queries

Add new field to mongo document and set incremental values

var cursor = db.getCollection("FacetTest").find();

var i = 1;
cursor.forEach(function(x){
    db.getCollection("FacetTest").update({_id: x._id}, {$set:{Order: NumberInt(i)}}, {
     upsert: false,
     multi: false
    });
    i++;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment