我正在尝试使用 MongoDB 重新分配 ID。但是,它没有将 ID 设置为等于我分配的值,而是创建了一个新的 ObjectId。如何分配自己的 ID?
> db.pGitHub.find();
{ "_id" : ObjectId("516f202da1faf201daa15635"),
"url" : { "raw" : "https://github.com/Quatlus",
"domain" : "github.com", "canonical" : "https://github.com/quatlus" } }
{ "_id" : ObjectId("516f202da1faf201daa15636"),
"url" : { "raw" : "https://github.com/Quasii",
"domain" : "github.com", "canonical" : "https://github.com/quasii" } }
> db.pGitHub.find().forEach(function(myProfile) {
var oldId = myProfile._id;
myProfile._id = 'exampleid';
db.pGitHub.save(myProfile);
db.pGitHub.remove({_id: oldId});
});
> db.pGitHub.find();
{ "_id" : ObjectId("516f204da1faf201daa15637"),
"url" : { "raw" : "https://github.com/Quatlus",
"domain" : "github.com", "canonical" : "https://github.com/quatlus" } }
{ "_id" : ObjectId("516f204da1faf201daa15638"),
"url" : { "raw" : "https://github.com/Quasii",
"domain" : "github.com", "canonical" : "https://github.com/quasii" } }
我正在使用 Mongo 2.4.2