MongoDB query nested array knowing parent id element (JavaScript)

If I have a MongoDB collection that looks like this:

_id: ObjectId(" <a objectid> ")
id: " <a userid> "
__v: 0
subscribedTo: Object

With or without Mongoose, how can I query the first child of object subscribedTo, which is called infl and it’s an array?

what I’m trying is this, but it says “infl” is undefined:

const foundUser = await Sub.findOne ({ "id" : userId });
console.log("this "+JSON.stringify(foundUser.subscribedTo.infl))

Although when I console log only foundUser.subscribedTo it correctly logs:

this [{"infl":["item1","item2"],"inflId":["item1id","item2id"]}]


According to your code output, subscribedTo is not an object,

it’s an array of objects. So, what you need is :


This will solve your issue for this case.