Kurs Google
Kurs Google

Reputation: 309

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

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
            ]
        }
    ]
}

Upvotes: 2

Views: 132

Answers (1)

Himanshu Sharma
Himanshu Sharma

Reputation: 3010

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"
                }
            ]
        }
    ]
}

Upvotes: 3

Related Questions