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.
{}event.update({}, { $pull: { members: { $elemMatch: { userId: user._id } } } }, functionMaybe you can also haveevent.update({}, { $pull: { members: { userId: user._id } } }, functionwithout$elemMatchevent.update({}, { $pull: { members: { userId: user._id } } }, function- greybutton