0

I have updated my Meteor.users() collection to include a 'profile' subdoc and inside profile I have an array of objects called 'notifications'. The object structure for notifications looks like this:

           'profile.notifications': {
            prompt: prompt,
            message: message,
            hasBeenRead: false,
            timestamp: Messages.find({}, {sort: {"ts": 1}, limit: 0}).fetch().pop().ts,
           }

Is there a way, using one of Mongo's methods, to change hasBeenRead to true for all of the objects within the array? I can change just one element using array indexes (the code below), but I can't use this approach to change all of them at once.

    Meteor.users.update({
       _id:  Meteor.userId() 
     }, {
        'profile.notifications.0.hasBeenRead': true //only changes first elem
        }
     })

1 Answer 1

1

There is currently no way to update all elements of an array through mongo directly. You can either send multiple updates to mongo or extract the notifications array, update all the elements, then update the document in one go.

var notifications = Meteor.user().profile.notifications;
notifications.forEach(function(e,i,a){a[i].hasBeenRead=true});
Meteor.users.update({ _id: Meteor.userId() },
  { $set: { 'profile.notifications': notifications }});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.