'Mongoose Find One on Nested Object
I'm trying to get info from an object that is nested within an object in Mongo. The data structure looks like this:
Card{
_id;
contributors: [
{
name;
_id;
},
{
name;
_id;
}
]
}
Here is my attempt at accessing a specific 'contributor' in the 'contributors' array.
Card.findOne({_id: cardId, "contributor._id": contributorId},
(err, contributor) => {
if (err) {
console.log(err);
res.status(500);
res.send({status: "error", message: "sass overload"});
return;
}
console.log(contributor);
res.send(contributor);
});
Solution 1:[1]
You need to use "contributors._id" not "contributor._id"
The name of the field in your Model is contributors not contributor.
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 | Teodor Sandu |
