Mongodb:克隆集合中的所有文档

时间:2014-03-01 00:40:37

标签: mongodb mongodb-query

我有一个mongoDB集合,我想要克隆同一个集合中的所有文档,但是所有新文档都应该有一个独特的" _id"字段。

Document within my Collection 'event'
{
"_id" : ObjectId("fsfd324324324wer"),
"active" : false,
"channels" : [ 
    "1", 
    "2"
],
"created" : ISODate("2013-06-16T00:25:57.781Z"),
"curationId" : ObjectId("51bd0612b0bf4e7035bfea00"),
"deleted" : true,
"lower_name" : "garden",
"name" : "Garden",
"user" : "abc"

}

我尝试的查询如下:

    db.event.find().forEach(function (x) {
    delete x._id;
    db.event.insert(x);
});

1 个答案:

答案 0 :(得分:1)

我假设集合名称为myCollection。

db.myCollection.find().forEach(function (x) {
    delete x._id;
    db.myCollection.insert(x);
});
相关问题