我们有一些文档最初使用StringObjectIdGenerator类型来存储_id。新数据将使用默认的ObjectIdGenerator保存。我们希望能够通过将_id转换为新数据类型来迁移现有数据。这种方式可行吗?
db.collection.initializeUnorderedBulkOp()
进行批量插入/删除。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
}
)
示例
> 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") }
>
db.inventries.find({ _id: { $type: 2 } }).forEach(function (d) { var id = ObjectId(d._id); var oldId = d._id; d._id = id; db.inventries.insert(d); db.inventries.remove({ _id: oldId }); } )
// change inventries => your collection name
db.getCollection('Files').find({"_id":{"$exists":true}}).forEach(function(x){
let id;
try {
id = new ObjectId(x._id);
}
catch (err) {
id = new ObjectId();
}
db.Files.update( { "_id": x._id}, { $set: { _id:id } } );
}
)