简体   繁体   中英

MongoDB: Updating A specific array element in a sub document

I'm a novice with mongodb so please excuse me if the question is a little basic. I have a mongo collection with a relatively complex document structure. The documents contain sub documents and arrays. I need to add additional data to some of the documents in this collection. A cut down version of the document is:

    "date" : ISODate("2018-08-07T08:00:00.000+0000"), 
    .
    . <<-- Other fields
    .
    "basket" : 
   [
     {
            "assetId" : NumberInt(639), 
            "securityId" : NumberInt(12470), 
            .
            . <<-- Other fields
            .
            "exGroup" : [
                . << -- Fields......
                .
                . << -- New Data will go here
            ]
     }
     .
     . << More elements

   ]

The following (abridged) aggregation query finds the documents that need modifying:

   { 
        "$match" : {
            "date" : {
                "$gte" : ISODate("2018-08-07T00:00:00.000+0000"), 
                "$lt" : ISODate("2018-08-08T00:00:00.000+0000")
            }
        }
    }, 
    { 
        "$unwind" : {
            "path" : "$basket"
        }
    }, 
    { 
        "$unwind" : {
            "path" : "$basket.exGroup"
        }
    }, 
    { 
        "$project" : {
            "_id" : 1.0, 
            "date" : 1.0, 
            "assetId" : "$basket.assetId", 
            "securityId" : "$basket.securityId", 
            "exGroup" : "$basket.exGroup"
        }
    }, 
    { 
        "$unwind" : {
            "path" : "$exGroup"
        }
    }, 
    { 
        "$match" : {
            "exGroup.order" : {
                "$exists" : true
            }
        }
    }

For each document returned by the mongo query I need to (in python) retrieve a set of additional data from a SQL database and then append this data to the original mongo document as shown above. The set of new fields will be the same, the data will be different. What is not clear to me is how, once I have the data I go about updating the array values.

Could somebody give me a pointer?

Try this, it works for me!

mySchema.aggregate([
   //your aggregation code
],function(err, docList){
  //for each doc in docList
  async.each(docList, function(doc, callback){
    query = {$and:[{idField:doc.idField},{"myArray.ArrayId":doc.myArray.ArrayId}]}
    //Update or create field in array
    update = {$set:"myArray.$.FieldNameToCreateOrUpdate":value}}
    projection = {field1:1, field2:1, field3:1}
    mySchema.findOneAndUpdate(query, update, projection, function(err, done){
        if(err){callback(err,null)}
        callback(null,'done')
    })
  ,function(err){
    //code if error
    //code if no error
  }
})

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