Update multiple array values


#1

I got this record structure

    {
        "_id" : "CAaveDCnPiC3KE6XS",
        "user_id" : "3zjDhNBRMQPQSeTFa",
        "notifs" : [ 
            {
                "to" : "3zjDhNBRMQPQSeTFa",
                "from" : "cvatYfpebH4X45EvN",
               "seen": false,
                "postedAt" : 1431952371949.0000000000000000
            }, 
            {
                "to" : "3zjDhNBRMQPQSeTFa",
                "from" : "cvatYfpebH4X45EvN",
               "seen": false,
                "postedAt" : 1431952378382.0000000000000000
            },
           {
                "to" : "3zjDhNBRMQPQSeTFa",
                "from" : "cvatYfpebH4X45EvN",
               "seen": true,
                "postedAt" : 1431952378382.0000000000000000
            }
}

I want to update all the seen profperty inside notifs property to true,
I can’t find How to do this, I tried few things nothing worked

Notifications.update({user_id: "sss"}, {$set: {"notifs.seen": true} });

Notifications.update({user_id:this.userId,'notifs.seen': {$ne: true}  },{$set: {'notifs.$.seen': true}});

I have meteorhacks:aggregate package installed, any aggregate solutions are also welcome

Note: $elemmatch is not a solution because it updates only one object in array


Sort Collection by Array value
#2

Your solution using the .$. modifier seems to be the right way to do this. Unfortunately, it’s not supported in minimongo, which I’m pretty sure means you will need to use a raw collection (so available on the server only).

This post might help


#3

Or you can get the objects with Collection.find, modify the objects with usual JS techniques, then put them back in the DB with Collection.extend from my package https://atmospherejs.com/vjau/mongo-extend if performance is not an issue.