簡體   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