'How to find an element in array which is a subelement in the Schema
likes: {
count:{type: Number, default: 0},
authors:{type: [String], default: []}
}
This is schema for the likes section. And I want to look if the given username is in the authors array or not ? How to create query for this situation?
await Entry.findOne({ 'likes.authors': { $in: user.username },_id: entryId } )
I have created a query like this but I couldn't get the data while I have a data which has entryId and contains user.username inside of 'likes.authors'.
Solution 1:[1]
try this:
Entry.findOne({'likes.authors': {$elemMatch: {type: user.username}},_id:entryId});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ncgomez17 |
