Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change MongoDb _id from string to ObjectId

We have documents that were originally stored using the StringObjectIdGenerator type for _id. Newer data will be saved using the default ObjectIdGenerator. We would like to be able to migrate the existing data by converting the _id to the new data type. Is this possible?

like image 951
dprice Avatar asked Jan 17 '18 00:01

dprice


1 Answers

Yes, it's possible to change data type, we need to find all documents with string id, using the iterator we can clone a document by changing its _id, after cloning we just need to delete the old document.

Below is for a small document, for large collection you may need to use db.collection.initializeUnorderedBulkOp() for bulk insert/delete

db.i.find({_id : {$type : 2}}). //find all string _id
    forEach(function(d){
        var id = ObjectId(d._id); //_id to ObjectId
        var oldId = d._id; // _id
        d._id = id; 
        db.i.insert(d); // clone doc with new Id
        db.i.remove({ _id : oldId }); // delete old doc
        }
    )

Example

> db.i.insertMany([{},{},{},{_id : "4a5ec389fee4c182f509f3ba"}, {_id : "4b5ec389fee4c182f509f3ba"}])
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5a5ec9edfee4c182f509f3c9"),
        ObjectId("5a5ec9edfee4c182f509f3ca"),
        ObjectId("5a5ec9edfee4c182f509f3cb"),
        "4a5ec389fee4c182f509f3ba",
        "4b5ec389fee4c182f509f3ba"
    ]
}


> db.i.find()
{ "_id" : ObjectId("5a5ec9edfee4c182f509f3c9") }
{ "_id" : ObjectId("5a5ec9edfee4c182f509f3ca") }
{ "_id" : ObjectId("5a5ec9edfee4c182f509f3cb") }
{ "_id" : "4a5ec389fee4c182f509f3ba" }
{ "_id" : "4b5ec389fee4c182f509f3ba" }


> db.i.find({_id : {$type : 2}}).
... forEach(function(d){
... var id = ObjectId(d._id); 
... var oldId = d._id; 
... d._id = id; 
... db.i.insert(d); 
... db.i.remove({ _id : oldId }); 
... }
... )


> db.i.find()
{ "_id" : ObjectId("5a5ec9edfee4c182f509f3c9") }
{ "_id" : ObjectId("5a5ec9edfee4c182f509f3ca") }
{ "_id" : ObjectId("5a5ec9edfee4c182f509f3cb") }
{ "_id" : ObjectId("4a5ec389fee4c182f509f3ba") }
{ "_id" : ObjectId("4b5ec389fee4c182f509f3ba") }
> 
like image 106
Saravana Avatar answered Sep 30 '22 05:09

Saravana