MongoDB 3.2更新嵌入式阵列中的字段名称

时间:2017-07-19 10:32:35

标签: mongodb migration bulkupdate

我试图在MongoDB中进行i迁移。我已从内容更新了字段 - >的StringContent。现在我想更新所有使用新字段名称的记录。

这就是文档的样子。

{
"_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
"Type" : "Onboarding",
"Cards" : [ 
    {
        "_id" : LUUID("3f328a1c-658d-ee4e-8f06-561760eb5be5"),
        "Width" : 1,
        "Title" : "",
        "Type" : "Freetext",
        "Description" : "",
        "Content" : "This is a test" // -> "StringContent" : "This is a Test"
    }, 
    {
        "_id" : LUUID("2f328a1c-658d-ee4e-8f06-561760eb5be5"),
        "Width" : 1,
        "Title" : "",
        "Type" : "Freetext",
        "Description" : "",
        "Content" : "This is another test" //-> "StringContent" : "This is another Test"            
    }
],
"DocumentType" : "Template",
"History" : [ 
    {
        "Date" : ISODate("2017-07-13T12:03:01.620Z"),
        "ByUserId" : LUUID("4ecaa6ca-2ce6-f84c-81f3-28f8f0256e6e")
    }
],
"Name" : "Default Template"
}

我创建了这个脚本:

        var bulk = db.getCollection('OnboardingPortal').initializeOrderedBulkOp(),
    count = 0;

     db.getCollection('OnboardingPortal').find({"DocumentType": "Template"}).
forEach(function(doc) {
    doc.Cards.forEach(function(card) {

        if(card.hasOwnProperty("Content")){
            print(card);
            bulk.find({"_id": doc._id, "Cards._id": card._id}).update(
            {
                $set: {"Cards.$.StringContent": card.Content}
            });
            bulk.find({"_id": doc._id, "Cards._id": card._id}).update(
            {
                $unset: {"Cards.$.Content": 1}
            });

            count += 2;
            if(count % 500 == 0) {
                bulk.execute();
                bulk = db.getCollection('OnboardingPortal').initializeOrderedBulkOp();
            }
        }
    });
});
if ( count % 500 !== 0 ){
    bulk.execute();      
}    

这不会更新任何内容,但如果我更改bulk.operations - >像这样在数组上显式设置索引,它将完成这项工作。但只限一张卡:

bulk.find({"_id": doc._id, "Cards.1._id": card._id}).update(
            {
                $set: {"Cards.1.StringContent": card.Content}
            });
            bulk.find({"_id": doc._id, "Cards.1._id": card._id}).update(
            {
                $unset: {"Cards.1.Content": 1}
            }); 

我的脚本中缺少什么,因此可以迭代多个文档并更改内容 - >每张卡中的StringContent。 ?

修改

我在我的脚本中添加了bulk.getOperations();。这就是它的回报。它是否应该用索引替换$

/* 1 */
[
    {
        "originalZeroIndex" : 0.0,
        "batchType" : 2.0,
        "operations" : [ 
            {
                "q" : {
                    "_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
                    "Cards._id" : "1c8a323f-8d65-4eee-8f06-561760eb5be5"
                },
                "u" : {
                    "$set" : {
                        "Cards.$.StringContent" : "This is a cool test"
                    }
                },
                "multi" : false,
                "upsert" : false
            }, 
            {
                "q" : {
                    "_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
                    "Cards._id" : "1c8a323f-8d65-4eee-8f06-561760eb5be5"
                },
                "u" : {
                    "$unset" : {
                        "Cards.$.Content" : 1.0
                    }
                },
                "multi" : false,
                "upsert" : false
            }, 
            {
                "q" : {
                    "_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
                    "Cards._id" : "1c8a322f-8d65-4eee-8f06-561760eb5be5"
                },
                "u" : {
                    "$set" : {
                        "Cards.$.StringContent" : "This is a test"
                    }
                },
                "multi" : false,
                "upsert" : false
            }, 
            {
                "q" : {
                    "_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
                    "Cards._id" : "1c8a322f-8d65-4eee-8f06-561760eb5be5"
                },
                "u" : {
                    "$unset" : {
                        "Cards.$.Content" : 1.0
                    }
                },
                "multi" : false,
                "upsert" : false
            }
        ]
    }
]

0 个答案:

没有答案