1

my gamefamilies collection looks like this

{
    "_id": ObjectId('54cc3ee7894ae60c1c9d6c74'),
    "game_ref_id": "REF123",
    ..
    "yearwise_details": [
    {
        "year": 1,
        ...
        "other_details": [
            {
                "type": "cash",
                "openingstock": 988
                ..
            },
            {
                "type": "FLU",
                "openingstock": 555
                ..
            },
            ..other items
        ]
    },
    {
        "year": 2,
        ...

        "other_details": [
            {
                "type": "cash",
                "openingstock": 3000,
                ....
            },
            ...
            {
                "type": "ghee",
                "openingstock": 3000,
                ...
            },
            ..
        ]
    }
]
}

My update query

db.gamefamilies.update({"game_ref_id": "REF123", "teamname": "manisha","yearwise_details.year": 2, "yearwise_details.other_details.type": "ghee"}, {"$set": {"yearwise_details.0.other_details.$.openingstock": 555} });

Document is getting picked up correctly. I expect to update year 2's item type="ghee" but instead year 1's 2nd item (type FLU) gets updated. What am I doing wrong ?

Any help would be greatly appreciated.

regards Manisha

1
  • try to make {yearwise_details.1.other_details.$.openingstock": 555} Commented Jan 31, 2015 at 15:06

1 Answer 1

1

Unfortunately, there is not yet support for nested $ positional operator updates.

So you can hardcode the update with

db.gamefamilies.update({"game_ref_id": "REF123",
                        "teamname": "manisha",
                        "yearwise_details.year": 2,
                        "yearwise_details.other_details.type": "ghee"},
                       {"$set":
                         {"yearwise_details.1.other_details.$.openingstock": 555}});

But notice that the yearwise_details.1.other_details is hardcoding that you want the second value of the array (it is 0-indexed, so the 1 is referencing the second element). I am assuming you found the command you have in your question because it worked for the first element of the array. But it will only ever work on the first element and the command above will only ever work on the second element.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply. How can I get index of yearwise_details ? Also I believe if I make 'yearwise_details' as an object (instead of an array) then that might solve my issue as well. If I make it an object can I add new 'yearwise_details' - i.e. new year sub document easily ?
The only way to get the index of the yearwise_details that you are searching for, you would need to use the $ the way that you are using it for the other_details details array. Is it possible to change the other_details array from being an array of objects with a type field value of cash or ghee to being an object with cash as the field for the rest of the cash object and ghee being the field for the rest of the field object instead of specifying the type within the subdocument? Otherwise, you could turn yearwise_details into an object from an array, but it loses order.
And it would be a little more difficult. Instead of pushing the objects, you would have to $set the subdocument field with the value of the object you would have pushed.
Is it something like below you are saying: { "_id": ObjectId('54cc3ee7894ae60c1c9d6c74'), "game_ref_id": "REF123", .. "yearwise_details": [ { "year": 1, ... cash: { "openingstock": 988 .. }, ghee:{ "openingstock": 555 .. }, .. }, { "year": 2, ... cash: { "openingstock": 3000, .... }, ... ghee: { "openingstock": 3000, ... }, .. } ] }
yes that's very much possible. With this structure I will try all my queries again. What I observed is even I need to hard code index for other_details like yearwise_details.1.other_details.15

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.