Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
});
like image 671
Linx Avatar asked Sep 01 '26 23:09

Linx


1 Answers

You need to use "contributors._id" not "contributor._id"

The name of the field in your Model is contributors not contributor.

like image 85
MarkB Avatar answered Sep 04 '26 13:09

MarkB