Update all arrays


#1

Hi,

Let’s say I have this kind of db:

   "users" : [
        {
                "_id": "7ydsfsdfsdfsdfsdfgPT",
                "name" : "robert",
                "profile" : [
			"status" : true,
                            ......
                            ]


        }
        {
                 "_id": "hbztZ5A6jHCHqFgPT",
                "name" : "Bernard",
                "profile" : [
			"status" : true,
                            ......
                            ]
        }
        {
                "_id": "fyztZ5AfgdfgdfgdfgfT",
                "name" : "Eric",
                "profile" : [
			"status" : false,
                            ......
                            ]
        }
        ...
   ]

I am struggling to find the mongo query which would set all “status” to false. Is it even possible to update all arrays at once?

Thanks

Michael


#2

I believe you want:

Users.update(
   {},
   { $set: { "profile.$.status" : false } }
)

Note that updates affecting more than one document may only be done on the server.


#3

My array is in fact one level deeper, does that change something?

I have tried (server side):

   Meteor.users.update({},{ $set: { "profile.friends.$.invitation" : false } });

But it doesn’t work:

Exception in setTimeout callback: MongoError: The positional operator did not find the match needed from the query. Unexpanded update: profile.friends.$.invitation

#4

Ok found a way:

Meteor.users.update({“profile.friends”: {$elemMatch: {invitation: true}}}, {$set: {“profile.friends.$.invitation”: false}},{multi: true});