0

I have a requirement to update sub document of my mongoDB document which is nested as array of array as shown below in json structure:

{
    "_id": "53cf4e3dae92ac6561807f6d",
    "results": {
        "trips": [
            {
                "_id": 1,
                "name": "A",
                "address": [
                    {
                        "_id": 123,
                        "value": {
                            "allowed": true,
                            "optional": false
                        }
                    },
                    {
                        "_id": 456,
                        "value": {
                            "allowed": false,
                            "optional": false
                        }
                    }
                ]
            },
            {
                "_id": 2,
                "name": "B",
                "address": [
                    {
                        "_id": 789,
                        "value": {
                            "allowed": true,
                            "optional": false
                        }
                    },
                    {
                        "_id": 567,
                        "value": {
                            "allowed": false,
                            "optional": false
                        }
                    }
                ]
            }
        ]
    }
}

Now, I want to update results.trips.address._id =456 with new json element "value" object. I can think of using update query with $elemMatch but may be I am not using correctly so, it is not working for me. Also, I would be doing all this in java using spring data 1.4.x and mongoDB version is 2.6. Any help would be highly appreciated.

Query which I am using is:

db.collection.update({"_id" : ObjectId("53cf4e3dae92ac6561807f6d"), "results.trips.address":{$elemMatch : {"results.trips.address._id":456}}},{$set : {"results.trips.address.value":{"allowed": true,  "optional": true}}});
Developer G
  • 11
  • 1
  • 5
  • Please post your non-working update query. Otherwise we can only guess what you could be doing wrong. Also, please tell us what "not working" actually means. Does it do nothing at all or does it write something different than you would expect? – Philipp Sep 01 '14 at 07:13
  • Query which I am using is as follows: – Developer G Sep 01 '14 at 07:26
  • db.collection.update({"_id" : ObjectId("53cf4e3dae92ac6561807f6d"), "results.trips.address":{$elemMatch : {"results.trips.address._id":456}}},{$set : {"results.trips.address.value":{"allowed": true, "optional": true}}}); – Developer G Sep 01 '14 at 07:27
  • I think that you can't update with one query, because you can't use multiple positional. https://jira.mongodb.org/browse/SERVER-831 – Barno Sep 01 '14 at 10:58
  • Try: db.collection.update({"_id" : ObjectId("53cf4e3dae92ac6561807f6d"), "results.trips.address":{$elemMatch : {"_id":456}}},{$set : {"results.trips.address.$.value":{"allowed": true, "optional": true}}}) I dint try it. – Johny T Koshy Sep 01 '14 at 11:10
  • @Barno: You are right I tried putting multiple positional operators but mongoDB allows only one. Can you explain me how I can do this using nested queries if possible because I don't want to add much logic in java, it better to do it at MongoDb. – Developer G Sep 01 '14 at 11:34
  • @Johny: It didn't work because my requirement is to update sub document which is array of array and MongoDB allows only one postional operator. – Developer G Sep 01 '14 at 11:35
  • see this related question: http://stackoverflow.com/questions/4121666/updating-nested-arrays-in-mongodb – Johny T Koshy Sep 01 '14 at 12:03
  • @DeveloperG A solution could be `db.collection.update({"results.trips.address":{$elemMatch : {"_id":456}}},{$set : {"results.trips.0.address.1.value":{"allowed": true, "optional": true}}})` – Barno Sep 01 '14 at 12:21
  • @Barno : results.trips.0.address.1.value will work but my requirement is to have these values dynamic i.e. it can nth element of 1st array and nth element of 2nd array. nth can be decided by elemMatch. – Developer G Sep 01 '14 at 17:46
  • yeah, i understand your problem but i don't think you can do all your stuff in one query,because you can't use multiple positional. i hope to mistake – Barno Sep 01 '14 at 19:16

0 Answers0