1

i need address in first one object update if object id not match then insert in address array.

{
    "_id" : ObjectId("5e0707d856cd5a1abcb7832f"), 
    "address" : [ 
        {
            "_id" : ObjectId("5e0727e87a21f51d3c5bf3bb"),
            "fulladdress" : "aa",
            "location" : "aa",
            "name" : "aa",
            "landMark" : "aa"
        },
        {
            "_id" : ObjectId("5e0727e87a21f51d3c5bf3bb"),
            "fulladdress" : "bb",
            "location" : "bb",
            "name" : "bb",
            "landMark" : "bb"
        }
    ], 
} 

I have nested object like: { _id: 122, nested: [ { _id: 1, name: 1 }, { _id: 2, name: 2 } ] }.

I need if nested object getting { _id: 2, name: 2 } object then it will update like { _id: 2, name: 5 } is change otherwise append the document like

{ _id: 123, nested: { _id:1, name: 1 }, { _id: 2, name: 2 }, { id: 3, name: 3 } ] }

saketh
  • 803
  • 1
  • 10
  • 24
user3305208
  • 29
  • 1
  • 6
  • It's not really clear what you're asking, is there a query that you've tried that isn't working? Can you provide both a sample input document and what you expect the result to be? – Joe Dec 31 '19 at 00:13
  • sorry. i have nested object like "{_id:122,nested:[{_id:1,name:1},{_id:2, name:2}]}". I need if nested object getting "{_id:2,name:2}" object then it will update like "{_id:2,name:5}" is change otherwise append the document like "{_id:123, nested:{_id:1,name:1},{_id:2, name:2},{id:3,name:3}]}". – user3305208 Dec 31 '19 at 05:51
  • Here is an example of updating a nested object based on a query condition: [Update Specific Elements of an Array of Documents](https://docs.mongodb.com/v4.0/reference/method/db.collection.updateMany/#update-specific-elements-of-an-array-of-documents). – prasad_ Jan 01 '20 at 02:53
  • Here is the solution you are looking for https://stackoverflow.com/a/18024670/11082661 – saketh Jan 01 '20 at 03:53

0 Answers0