Howto add a value to all users document from a array?

I have a event (event1) where people can invite friends.

  1. Push invite button
  2. Se list of friends
  3. checkbox friends (eg. friend1 + friend2) (thepeople is a array friend1,friend2)
Events.upsert(
  { _id: eventid },
  { $addToSet:{ pendingId: { $each: thepeople } } }
);

Gives me:

"_id" : "event1",
"pendingId" : [ 
        "friend1", 
        "friend2"
    ]

Now i need to insert the eventid into all the users document

"userid" : "friend1",
"pendingId" : [ 
        "event1", 

    ]

"userid" : "friend2",
"pendingId" : [ 
        "event1", 

    ]

Howto do that?

User.update({ _id: { $in: thePeople }, { $push: { pendingId: event1 } }, { multi: true })

Doing this though is a brittle approach, and you are filling the user object with unneccesary data. Keeping the two in sync could be a pain also. What if you made an Invitation collection instead, containing a userId and an eventId, so the user subscribed to all Invitations with his userId?

1 Like

I did it like this:

      Events.upsert(
        { _id: eventid },
        { $addToSet:{ pendingId: { $each: thepeople } } }
      );

      Meteor.users.update(
        { _id: { $in: thepeople } },
        { $addToSet:{ pendingId: eventid } },
        { multi: true }
      );

In some articles and forums, people say that it is better to have some data close to the user and event documents

Now I need to find out howto filter the list with people so I dont invite people more then one time

That should not be possible, as you are using addToSet, where the definition of a “set” is that there are no duplicates.

The persons will go to another field when approve or decline the invite.

Field arrays

pendingId [user1,user2…]
approvedId [user3,user4…]
DeclinedID [user5,user6…]
MaybyId [user7,user8…]

so I need to find out howto search if a persons id is in pendingId OR approvedId OR DeclinedID OR MaybyId