'How to search for records by using a value in object array from mongoose?
I want to search for records using the below-mentioned Field and code I have used, returning nothing. I'm sure I'm querying wrongly, and can anyone help me build the correct query code?
Code I have used:
const { id } = req.params;
const requests = await Request.find({
itemsList: { item: id },
});
Return:
console.log(requests);
requests: []
Solution 1:[1]
Please try this
const requests = await Request.find({
itemsList: { $elemMatch: { item: mongoose.Types.ObjectId(id) }}},
});
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 | Mr.Gandhi |