0

I have collection the structure of which is :-

Subscribed.insert({
    "name":  "Manager1",
    "emailId": "arora.priya4172@gmail.com",
    "category": "Finance",
    "designation": 'Head',
    "done": false,
    "categorySubscribedUsers": [
        {
            "_id": "u4._id",
            "username": "u4.profile.name",
            "issuesNotToDisplay": []
        },
        {
            "_id": "u4._id",
            "username": "u4.profile.name",
            "issuesNotToDisplay": []
        },
        {
            "_id": "u4._id",
            "username": "u4.profile.name",
            "issuesNotToDisplay": []
        }
    ]
});

I want to insert a field in issuesNotToDisplay field. So, for this I am using the following command but getting error:

'syntax error: missing : after property id' in browser console and 'syntax error missing token .'

In the mongodb console.

Can anyone please tell me what should be the proper command for updating a field in doubly nested array in mongodb. I have tried a lot and read book too but still clueless. Why this command is giving error?

Command is :-

db.subscribed.update(
    {
        "category": "Finance",
        "categorySubscribedUsers": "priya"
    },
    { 
        "$addToSet": {
            "categorySubscribedUsers.$.issueNotToDisplay": "25PEgZoMamLSTDdw7"
        }
    }
);
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
priya
  • 81
  • 2
  • 10
  • Does this help: http://stackoverflow.com/questions/4121666/updating-nested-arrays-in-mongodb – Lalit Agarwal Apr 16 '14 at 11:47
  • You're actually using meteor, aren't you? – Neil Lunn Apr 16 '14 at 11:55
  • Also worth noting is that the positional `$` operator is completely invalid unless you are trying to match something within the array. Your sample will not possibly produce a match, unless you actually mean "categorySubscribedUsers.username" as the field to match – Neil Lunn Apr 16 '14 at 12:26
  • Now I am using following command but getting same error... db.subscribed.update({category:'Finance', categorySubscribedUsers.username:'priya'},{$addToSet:{categorySubscribedUsers.issueNotToDisplay:'SL9NchSjbsjLF2KBb'}}); – priya Apr 17 '14 at 12:21

1 Answers1

0

May be you need this

db.subscribed.update(
{
    "category": "Finance",
    "categorySubscribedUsers.username" : "u4.profile.name"
},
{ 
    "$addToSet": {
        "categorySubscribedUsers.$.issueNotToDisplay": "25PEgZoMamLSTDdw7"
    }
}
)
achuth
  • 1,212
  • 2
  • 16
  • 29
  • Hi, thanks achuth....This command is working fine. But I need to ask is if instead of mongo console. I run the following query (minor difference although), error is coming as "[16:15:14.522] Error: Not permitted. Untrusted code may only update documents by ID. [403]". Why it is so??........Command is :- Subscribed.update({"_id": "92vjyYGRoP4mZxTck","categorySubscribedUsers.username" : "abhi"},{ "$addToSet": {"categorySubscribedUsers.$.issueNotToDisplay": "7mto9WM4AmZxWc7YR"}). Even after using the id too, it is giving error....Thanks in advance – priya Apr 29 '14 at 15:17
  • Check this http://stackoverflow.com/questions/15464507/understanding-not-permitted-untrusted-code-may-only-update-documents-by-id-m – achuth Apr 30 '14 at 04:25
  • Please let me know what change need to be done as.......Subscribed.update({"_id":Subscribed.findOne({"category": "Finance", "categorySubscribedUsers.username" :"abhi"})._id},{ "$addToSet": {"categorySubscribedUsers.0.issueNotToDisplay": "25PEgZoMamLSTDdw7"}}) (working) but.... Subscribed.update({"_id":Subscribed.findOne({"category": "Finance", "categorySubscribedUsers.username" :"abhi"})._id},{ "$addToSet": {"categorySubscribedUsers.j.issueNotToDisplay": "25PEgZoMamLSTDdw7"}}) not working ... :(. But for code I need to insert the array index position. – priya May 08 '14 at 13:35
  • "categorySubscribedUsers.j.issueNotToDisplay" what is j over there? – achuth May 09 '14 at 02:29
  • I am using this query inside a loop, so here j indicates the loop index at which array index I can update the database. If you don't understand, I can share that function with you – priya May 12 '14 at 11:49
  • Can anyone please tell me how to proceed with this. Thanks in advance – priya May 13 '14 at 10:11