简体   繁体   English

更新MongoDB中的嵌套数组

[英]Update nested array in MongoDB

I'm trying to do an update here in the mongo shell and I'm having trouble. 我正在尝试在mongo shell中进行更新,但是遇到了麻烦。

I have the following json: 我有以下json:

{
"_id" : ObjectId("56cc03c16f4e85f538ef79ae"),
"contact_id" : NumberLong(1000295524418),
"gender" : 1,
"phonetic_gender" : 1,
"first_name" : "LEANDRO",
"score" : 44,
"address" : [ 
    {
        "address_id" : NumberLong(2634224807),
        "rank" : 201604.0,
        "score" : 7.0,
        "street_type" : "AV",
        "street_title" : "DA",
        "street" : "EMILIA DE CASTRO MARTINS",
        "number" : 34.0,
        "district" : "JARDIM BELA VISTA",
        "city" : "GUARULHOS",
        "state" : "SP",
        "zip_code" : 7132470.0,
        "create_date" : ISODate("2014-08-07T00:00:00.000Z"),
        "update_date" : ISODate("2016-05-03T00:00:00.000Z")
    }, 
    {
        "address_id" : NumberLong(2634735566),
        "rank" : 201410,
        "score" : 10,
        "street_type" : "AV",
        "street_title" : "DA",
        "street" : "EMILIA DE CASTRO MARTINS",
        "district" : "JARDIM BELA VISTA",
        "city" : "GUARULHOS",
        "state" : "SP",
        "zip_code" : "07132470",
        "create_date" : ISODate("2014-08-07T03:00:00.000Z"),
        "update_date" : ISODate("2014-08-07T03:00:00.000Z")
    }
]}

I need to go through all my documents and update the type of field "rank" and "score" in the address array. 我需要浏览所有文档,并更新地址数组中字段“等级”和“得分”的类型。

See the following code I'm doing: 请参阅我正在执行的以下代码:

var total = 0
var skip = 0
var total_adress = db.company.count() - skip
var bulk = db.person.initializeUnorderedBulkOp()
var person = db.getCollection('Person').find( {$and:[ {"contact_id":1000295524418}).addOption(DBQuery.Option.noTimeout).forEach(
function(person){
        var contact_id = person.contact_id.valueOf()
            bulk.find(
                { contact_id: contact_id  }
            ).update(
                { 
                    $set: {
                        "address.$.zip_code":"address.zip_code".toString(),
                        "address.$.rank": NumberInt("address.rank"),
                        "address.$.number": "address.number".toString(),
                        "address.$.score": NumberInt("address.score") - 2
                    }
                } 
            );


    if((++total % 1000) === 0){
        print("Total person....: " + total_adress)
        print("Iniciando bulk..: " + Date())
        bulk.execute({ w: 0 })
        bulk = db.company.initializeUnorderedBulkOp()
        print("Fim bulk........: " + Date())
        print("#############################################################################")
    }
}); bulk.execute();  print(total);

Now comes the problem, when I run this command in Mongo it does not error. 现在出现了问题,当我在Mongo中运行此命令时,它不会出错。 Have made sure that it falls within the foreach and and search my data in the field correctly, the problem is just the update does not work. 确保它落入foreach并在字段中正确搜索我的数据,问题只是更新不起作用。

Thank you! 谢谢!

The address key in the person document is an array of documents. 个人文档中的地址键是一组文档。 Based on jstell's comment, one of the solutions is to perform the following steps: 根据jstell的评论,解决方案之一是执行以下步骤:

  • Make a copy of the address array into array_of_addresses variable. 将地址数组的副本复制到array_of_addresses变量中。
  • Loop through each document of array_of_addresses and update the keys as necessary. 遍历array_of_addresses每个文档,并根据需要更新密钥。
  • In bulk.find.update pipeline, update the address key value with newly updated array array_of_addresses . bulk.find.update管道中,使用新更新的数组array_of_addresses更新address键值。

Please note, that any update to the address array between find() and the execution of bulk update may be overwritten. 请注意,在find()和批量更新执行之间对地址数组的任何更新都可能被覆盖。

Other minor changes also suggested: 其他较小的更改还建议:

  • getCollection('person') instead of getCollection('Person') getCollection('person')而不是getCollection('Person')
  • Missing square/curly brackets in find command: find( {$and:[{"contact_id":1000295524418} ]} ) find命令中缺少方括号/圆括号: find( {$and:[{"contact_id":1000295524418} ]} )

     var total = 0; var skip = 0; var bulk = db.person.initializeUnorderedBulkOp(); db.getCollection('person').find({$and:[{"contact_id"1000295524418}]}).addOption(DBQuery.Option.noTimeout).forEach( function(person){ //extract the array into array_of_addresses variable var contact_id = person.contact_id.valueOf(); var array_of_addresses = person.address.valueOf() ; //Loop through the array_of_addresses for ( var i = 0; i< array_of_addresses.length ; i++) { //assign element of array to address variable address = array_of_addresses[i]; //DO YOUR MODIFICATIONS HERE. eg zipcode zip_code = address.zip_code.valueOf().toString(); address.zip_code = zip_code; } //Set the modified array value to address element of the document bulk.find( { contact_id: contact_id } ).update( { $set: { address : array_of_addresses } } ); }); //bulk execute bulk.execute(); 

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

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