Replies: 1 comment 1 reply
-
Unfortunately not currently possible to filter for all the subdocuments that match. You can filter for just the first subdocument that matches using elemMatch in projection: https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/ |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am using findOne() to filter record base on the field of the nested subdocument but findOne() and find() are returning all the records instead of filtering it by the subdocument field.
WHAT I WANT TO ACHIEVE
I want to return the records that match only the subdocument field used in the query filter.
WHAT I HAVE TRIED
THE RETURNED DATA
QUERY 1
QUERY 2
QUERY 3
Query 1 and Query 3 worked but they returned all the records in the subdocuments(user1, user2, user3) rather than just filtering the query by user2 but QUERY 2 did not work at all as it showed syntax error saying that "user2" is not defined(user2 that is referenced in the $elemMatch). I actually wanted to use aggregate but I am populating records from other Models with the find() and findOne() but I do not know how to populate fields from other Model with aggregation.
All the examples i have been seeing are using MongoDB but not Mongoose.
I will appreciate your kind assistance.
Beta Was this translation helpful? Give feedback.
All reactions