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"
                }
            ]
       
    }
Sriram
  • 101
  • 9
  • This post updates with a condition: [Not able to pull from nested array and query return sub-document using MongoTemplate](https://stackoverflow.com/questions/60510065/not-able-to-pull-from-nested-array-and-query-return-sub-document-using-mongotemp/60520266#60520266) – prasad_ Mar 15 '21 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 ? – Sriram Mar 15 '21 at 14:37
  • Then, use the [filtered positional operator](https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/index.html) with the update. – prasad_ Mar 15 '21 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" } ] } ) – Sriram Mar 15 '21 at 15:14
  • Is there any way to do like in a single short? – Sriram Mar 15 '21 at 15:19
  • See this post which uses the _filtered positional operator_: [How to add a json in a nested array of a mongodb document using Spring?](https://stackoverflow.com/questions/60701824/how-to-add-a-json-in-a-nested-array-of-a-mongodb-document-using-spring/60962573#60962573) – prasad_ Mar 15 '21 at 16:13

0 Answers0