1

I want to add to all documents in myCollection a field named "serie" with a serial integer number, for example:

{"_id" : "507f191e810c19239de098db", "name" : "John", "serie" : "1"}
{"_id" : "507f191e810c19729de860ea", "name" : "Dave"}, "serie" : "2"}
{"_id" : "507f191e810c19729de564ou", "name" : "Kate"}, "serie" : "3"}
......

The objectId (_id) is not sexy to display or to remember by humains, so I want to keep it and add an other field 'serie' containing simple, short, and unique numbers to identify every doc, like a serial number. So I tried the following script but I got the same "serie" value in all docs:

for(var i=1; i < 543; i++){    
  db.myCollection.update({},
    { $set: {"serie": i}},
    { upsert:false, multi: true });
}

Thank you for your help.

2
  • This question is equally a lot broader than you think as well as being very unclear for your purpose. What sort of order do you want to add this increasing field in? Have you thought about how you would even do such a thing in something like a SQL database and how would you think the process would be different here? Are you even aware that the existing _id value you show here is a both a Primary key of unique values and is monotonic ( ever increasing ) already? Commented May 23, 2014 at 22:22
  • @NeilLunn , Sorry for not being clear, I edited my question, thank you Commented May 24, 2014 at 8:50

2 Answers 2

6

The problem with what you are doing here is essentially the multi: true part of your statement. Essentially you are saying update "everything" with this number, 543 times.

To get an increasing number do this:

var i = 1;
db.myCollection.find().forEach(function(doc) {
    db.myCollection.update(
        { "_id": doc._id },
        { "$set": { "serie": i } }
    );
    i++;
)

Or faster still with batches from MongoDB 2.6 and onwards

var i = 1;

var cmd = { 
    "update": "myCollection",
    "updates": []
};

db.myCollection.find().forEach(function(doc) {

    cmd.updates.push({
        "q": { "_id": doc._id },
        "u": { "$set": { "serie": i } }
    });

    if ( batch.length % 500 == 0 ) {
        db.runCommand(cmd);
        cmd.updates = [];
    }
    i++;

});

if ( cmd.updates.length > 0 )
    db.runCommand(cmd);

So not only faster due to not pulling the write acknowledgement on every update but also because it is sending updates in batch sizes of 500 at a time.

Sign up to request clarification or add additional context in comments.

Comments

2

Thank you, I got it, I just added NumberInt(i) to your answer to get {"serie":"1"} instead of {"serie":"1.00000"}

var i = 1
db.myCollection.find().forEach(function(doc) {
    db.myCollection.update(
        { "_id": doc._id },
        { "$set": { "serie": NumberInt(i)} }
    );
    i++;
    })

Thank you Neil for your help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.