3

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);
});
Linx
  • 753
  • 3
  • 15
  • 34

1 Answers1

5

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

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

Teodor Sandu
  • 1,348
  • 1
  • 20
  • 31
MarkB
  • 1,783
  • 2
  • 17
  • 32