繁体   English   中英

更新MongoDB中的嵌套数组

[英]Update nested array in MongoDB

我正在尝试在mongo shell中进行更新,但是遇到了麻烦。

我有以下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")
    }
]}

我需要浏览所有文档,并更新地址数组中字段“等级”和“得分”的类型。

请参阅我正在执行的以下代码:

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

现在出现了问题,当我在Mongo中运行此命令时,它不会出错。 确保它落入foreach并在字段中正确搜索我的数据,问题只是更新不起作用。

谢谢!

个人文档中的地址键是一组文档。 根据jstell的评论,解决方案之一是执行以下步骤:

  • 将地址数组的副本复制到array_of_addresses变量中。
  • 遍历array_of_addresses每个文档,并根据需要更新密钥。
  • bulk.find.update管道中,使用新更新的数组array_of_addresses更新address键值。

请注意,在find()和批量更新执行之间对地址数组的任何更新都可能被覆盖。

其他较小的更改还建议:

  • getCollection('person')而不是getCollection('Person')
  • 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