最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add a field in all documents in an existing collection (mongodb)? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jun 26, 2017 at 13:55 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked May 23, 2014 at 22:10 user2981029user2981029 5841 gold badge6 silver badges17 bronze badges 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? – Neil Lunn Commented May 23, 2014 at 22:22
  • @NeilLunn , Sorry for not being clear, I edited my question, thank you – user2981029 Commented May 24, 2014 at 8:50
Add a ment  | 

2 Answers 2

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论