There is an event model in which the data on e participants are partially denormalized, and in the array event.members are stored not _id, but objects

[ { userId: _userName: _userAvatarThumbsPath .... }, { ... }, { ... } ] 

The following code is used to update the data (I provide the main part)

  ... async.parallel({ user: function ( callback ) { user.update( { $pull: { joinedEvents: event._id } }, function ( err, affected ) { callback( err, user._id ); }); }, event: function ( callback ) { event.update( { $pull: { members: { $elemMatch: { userId: user._id } } } }, function ( err, affected ) { callback( err, true ); }); } }, callback ); ... 

If normal strings are stored in user.joinedEvents, then there are no problems with deleting them, and everything works adequately. And when using the $ elemMatch operator when one participant exits, the method clears the entire event.members array. That is, the participants join, join, and then one came out and the list of participants is empty.

I use NodeJS: 7.4.0 Mongoose: 4.6.8 Thanks in advance for the hints.

  • $ pull is a delete command. you may need $ push - greybutton
  • Maybe I was not quite clear. I need to remove those particular members who have left the community. Instead, when one participant exits, not one record is deleted, but all from the event.members array. - Alexandr Sydorenko
  • Try adding {} event.update({}, { $pull: { members: { $elemMatch: { userId: user._id } } } }, function Maybe you can also have event.update({}, { $pull: { members: { userId: user._id } } }, function without $elemMatch event.update({}, { $pull: { members: { userId: user._id } } }, function - greybutton
  • I tried before I saw your comment, but thanks anyway. Works without $ elemMatch - Alexandr Sydorenko

1 answer 1

 event.update( { $pull: { members: { userId: user._id } } }, function ( err, affected ) { callback( err, true ); });