2016-05-16 46 views
0

我是Mongo Db的新手,嘗試迭代mongo db集合中的文檔並插入同一文檔的副本。在mongodb集合中查找並插入未能插入完整文檔

下面是一個示例文件

{ 
    "_id" : ObjectId("573a15351f7409771c330acd"), 
    "internal" : { 
     "id" : "562e0cade4b0d50120cee6c1" 
    }, 
    "status" : "ACTIVE", 
    "integrationKeys" : { 
     "associationId" : "dev-aws_moratuwauni" 
    }, 
    "external" : { 
     "id" : "8a510sba2-2c0d-4e87-9145-daf33cfcff36" 
    }, 
    "links" : [ ], 
    "createdDate" : "2015-10-26T11:10:17+0000", 
    "updatedDate" : "2015-10-26T11:10:17+0000", 
    "isManuallyCreated" : true 
} 

我在蒙戈DB殼嘗試此。這是我循環和做插入。

db.courses.find({ $and: [{"isManuallyUpdated": {$ne: true}}, {"isManuallyCreated": {$ne: true}}]}).limit(1).forEach(function(doc) 
{ 
    $set: {doc._id = ObjectId()}; 
    $set: {doc.integrationKeys.associationId = "dev-kandyuni"}; 
    $set: {doc.isManuallyCreated = true}; 
    db.courses.insert(doc) 
}); 

這創建了一個新文檔,但只有2個鍵值如下所示。

{ 
    "_id" : ObjectId("573a149e1f7409771c330acb"), 
    "isManuallyCreated" : true 
} 

我也試過db.courses.save(doc),不幸的結果是一樣的。有些人可以幫忙。

回答

0

問題在於我用來檢索數據的查詢。新的查詢是

db.courses.find({ $and: [{"integrationKeys.associationId": "dev-kandyuni"}, {"isManuallyCreated": true}]}); 
相關問題