简体   繁体   English

MongoDB:更新子文档中的特定数组元素

[英]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. 我是mongodb的新手,所以如果问题有点基本,请原谅。 I have a mongo collection with a relatively complex document structure. 我有一个具有相对复杂的文档结构的mongo集合。 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. 对于mongo查询返回的每个文档,我需要(在python中)从SQL数据库中检索一组附加数据,然后将此数据附加到原始mongo文档中,如上所示。 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
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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