简体   繁体   中英

Find and insert in mongodb collection fail to insert complete document

I am new to Mongo Db and trying to iterate through documents in a mongo db collection and insert a copy of that same document.

Following is a sample document

{
    "_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
}

I'm trying this in mongo db shell. This is how I loop and do the insert.

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)
});

This create a new document, but only with 2 key-values as shown below.

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

I also tried db.courses.save(doc), unfortunately result was same. Can some please help.

Issue was with the query that I have used to retrieve data. New query is

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM