mongoose: MongooseArray.pull Does Not Work on Subdocuments
When using MongooseArray.pull to remove a subdocument from an array using a command such as:
someUser.attending.pull({event_id : x, user_id : y})
Mongoose will remove all subdocuments from the array that either have event_id
set to x
OR user_id
set to y
. This is unexpected since MongoDB’s $pull
would remove only subdocuments that have event_id
set to x
AND user_id
set to y
.
If this is a feature, this should be clearly documented.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 16
Commits related to this issue
- docs; clarify Array.pull() re: #3341 — committed to Automattic/mongoose by vkarpov15 9 years ago
- repro; #3341 — committed to Automattic/mongoose by vkarpov15 9 years ago
There was new code added to solve the issue. Before, mongoose would not handle
pull()
correctly on arrays with documents that don’t have an_id
. The fix for this issue was a fallback to a deep equality check (which mongodb does internally, not mongoose) when there’s no_id
.My subdocuments do not explicitly contain an
_id
field. For instance, I declared the UserSchema as follows:I would also like to point out that the information you just provided about how
.pull()
is supposed to be used it not available on the documentation at all. For instance, how.pull()
behaves with and without an_id
field.