0

Unfortunatelly all my Mongo collections have _id as integer, instead of Object ID. I'm trying to amend that with Robomongo client, and I'm trying with that command:

db.getCollection('client').find({}).forEach(function(doc){
  var doc2 = doc;
  doc2._id = new ObjectId(); 
  db.collection1.insert(doc2);
});

Unfortunatelly the documents are not inserted or changed, although I get this output:

 Inserted 1 record(s) in 1ms
 Inserted 1 record(s) in 1ms
 Inserted 1 record(s) in 1ms
 Inserted 1 record(s) in 1ms
 Inserted 1 record(s) in 1ms
 Inserted 1 record(s) in 1ms

How should I change the command above to make it working?

user1883212
  • 7,539
  • 11
  • 46
  • 82
  • Why not just `delete doc._id; db.collection1.insert(doc);` ? btw how do you check if they were inserted? I can't reproduce tbh. – Alex Blex Mar 27 '17 at 13:37
  • Are you trying to _only_ update your `client` collection or copy items from `client` to `collection1` ? Because currently you are copying items in `client` to `collection1` and updating there _id in the same time .. Not sure that what you wanted – Arount Mar 27 '17 at 13:39
  • The [answer](http://stackoverflow.com/a/16951488) by Patrick Wolf will work for you. – chridam Mar 27 '17 at 13:43
  • I just need to change the field _id from int to objectId – user1883212 Mar 27 '17 at 14:04

0 Answers0