0

How to use spring mongo query criteria based only for below thing.

I don't want to use spring mongo save method even though its doing upsert because of application is running in multi threaded environment so only specified values should updated and remaining values will not change.

Suppose the document looks like:

{
        "dept": "FY",
        "companyName": "TIS",
        "id":"112234",
            "contacts": [{
                    "name": "Jeff",
                    "phone": "222 - 572 - 8754"
                },
                {
                    "name": "Joe",
                    "phone": "456 - 875 - 4521"
                },
                
                {
                    "name": "Alex",
                    "phone": "789 - 875 - 4521"
                }
            ]
       
    }

based on id and the array list passing to update method the contacts list should update.

Lets take based on id=112234 and passing contacts list to update method, based on contact name it should update.

"contacts": [{
            "name": "Jeff",
            "phone": "111 - 000 - 123"
        },
        {
            "name": "Joe",
            "phone": "980 - 546 - 0000"
        }
        ]

the final mongo json document should updated as below:

{
        "dept": "FY",
        "companyName": "TIS",
        "id":"112234",
            "contacts": [{
                    "name": "Jeff",
                    "phone": "111 - 000 - 123"
                },
                {
                    "name": "Joe",
                    "phone": "980 - 546 - 0000"
                },
                
                {
                    "name": "Alex",
                    "phone": "789 - 875 - 4521"
                }
            ]
       
    }
6
  • This post updates with a condition: Not able to pull from nested array and query return sub-document using MongoTemplate Commented Mar 15, 2021 at 10:30
  • @prasad_,provided link is updating only single element in array , but i have multiple matched elements need to be update in a single query. Is it possible to do ? Commented Mar 15, 2021 at 14:37
  • Then, use the filtered positional operator with the update. Commented Mar 15, 2021 at 14:41
  • even though filtered positional operator setting only single value for example db.getCollection('ss').update( { }, { $set: { "contacts.$[element].phone" : '111 - 000 - 123' } }, { multi: true, arrayFilters: [ { "element.name": "Jeff" } ] } ) but i need like this way db.getCollection('ss').update( { }, { $set: { "contacts.$[element].phone" : '111 - 000 - 123' } }, { $set: { "contacts.$[element].phone" : '980 - 546 - 0000' } }, { multi: true, arrayFilters: [ { "element.name": "Jeff" } ,{ "element.name": "Joe" } ] } ) Commented Mar 15, 2021 at 15:14
  • Is there any way to do like in a single short? Commented Mar 15, 2021 at 15:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.