vencrena
vencrena

Reputation: 65

Mongo db update statement

I have simple document:

 /* 1 */
{
    "_id" : "9cbfceec-d6f6-5638-b7c9-49103dc39665",
    "Settings" : [ 
        [ 
            "Storage", 
            "Dedicated"
        ], 
        [ 
            "Country", 
            "EE"
        ], 
        [ 
            "Address", 
            "http://localhost:55557"
        ], 
        [ 
            "Number", 
            "05"
        ]
    ]
}

can't figure out how to write update statement to update address information "http://localhost:55557" to IP address.something like http://10.10.20.2:66667

Upvotes: 0

Views: 151

Answers (2)

bhuvnesh
bhuvnesh

Reputation: 21

chirdam is right about schema .

If you don't have provision to make new schema you can try following :

Try this query :

db.yourCollection.update({"_id" : "9cbfceec-d6f6-5638-b7c9-49103dc39665", "Settings.Address" : "http://localhost:55557"} , { $set : {"Settings.$.Address" : "http://10.10.20.2:66667"}});

Or alternative is use RoboMongo to manually edit your collection

Upvotes: 0

White Bullet
White Bullet

Reputation: 215

The schema design is incorrect, as there is no key for the value that you want to update. Consider changing your schema as follows

{
_id : "9cbfceec-d6f6-5638-b7c9-49103dc39665",
settings : 
    {  
        storage:"Dedicated", 
        country:"EE",
        address:"http://localhost:55557",
        number:"05"
    }
}

The above schema has key for every value in settings rather than having a double array. Any value is accessible and editable in the above document.

You can then update the document as follows

db.settings.update({
  _id: "9cbfceec-d6f6-5638-b7c9-49103dc39665"
}, {
  $set: {
    "settings.address": "http://10.10.20.2:66667"
  }
});

Upvotes: 3

Related Questions