How to update in one query, multiple times without sharing to simple queries?

两盒软妹~` 提交于 2020-01-04 09:16:09

问题


I have the following model:

{
    "_id": "unique1",
    "companyBases": [
        {
            "_id": "company base 1",
            "vehicles": [
                   ....some objects
            ]
        },
        {
            "_id": "company base 2",
            "vehicles": [
                   ....some objects
            ]
        },
        {
            "_id": "company base 3",
            "vehicles": [
                   ....some objects
            ]
        }
    ]
}

I would like to update this document by overwritting "vehicles" array in some "companyBases" that matches. This is my input:

[
    {
        "_id": "company base 1",
        "vehicles": [ 
               array thats overwrites old array "vehicles", where _id of object in "companyBases" is equeal to "company base 1"
               ....some new objects
        ]
    },
    {
        "_id": "company base 3",
        "vehicles": [
               array thats overwrites old array "vehicles", where _id of object in "companyBases" is equeal to "company base 2"
               ....some new objects
        ]
    }
]

I would like to update multiple times by using one query, not multiple simple queries - update with $set (if it's possible), because it could impact on performance with update 200-300 times per one request.

Expected output (in model):

{
    "_id": "unique1",
    "companyBases": [
        {
            "_id": "company base 1",
            "vehicles": [
                   ....some new objects
            ]
        },
        {
            "_id": "company base 2",
            "vehicles": [
                   ....some objects
            ]
        },
        {
            "_id": "company base 3",
            "vehicles": [
                   ....some new objects
            ]
        }
    ]
}

回答1:


We can prepare multiple array filters in the same query. An array filter identifies the array elements which match that filter. The following is an example:

db.collection.update(
    {"_id":"unique1"},
    {
        $set:{
            "companyBases.$[filter1].vehicles":[
                {
                    "tag":"new1"
                }
            ],
            "companyBases.$[filter2].vehicles":[
                {
                    "tag":"new2"
                }
            ]
        }
    },
    {
        "arrayFilters":[
            {
                "filter1._id":"company base 1"
            },
            {
                "filter2._id":"company base 3"
            }
        ]
    }
)

Data set:

{
    "_id": "unique1",
    "companyBases": [
        {
            "_id": "company base 1",
            "vehicles": [
                  {
                    "tag":"old"
                  }
            ]
        },
        {
            "_id": "company base 2",
            "vehicles": [
                  {
                    "tag":"old"
                  } 
            ]
        },
        {
            "_id": "company base 3",
            "vehicles": [
                   {
                    "tag":"old"
                  } 
            ]
        }
    ]
}

Output:

{
    "_id" : "unique1",
    "companyBases" : [
        {
            "_id" : "company base 1",
            "vehicles" : [
                {
                    "tag" : "new1"
                }
            ]
        },
        {
            "_id" : "company base 2",
            "vehicles" : [
                {
                    "tag" : "old"
                }
            ]
        },
        {
            "_id" : "company base 3",
            "vehicles" : [
                {
                    "tag" : "new2"
                }
            ]
        }
    ]
}


来源:https://stackoverflow.com/questions/57699721/how-to-update-in-one-query-multiple-times-without-sharing-to-simple-queries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!