1

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"]}]
2
  • What is .subscribedTo.infl ? What do you get if you only console log foundUser ? Commented Dec 13, 2020 at 15:17
  • infl is an array, first child of subscribedTo which is an object. If i console log JSON.stringify(foundUser) i get the whole json i shown at the top: this {"_id":" xxx","id":" xxx ","__v":0,"subscribedTo":[{"infl":["item1","item2"],"inflId":["item1id","item2id"]}],"updatedAt":"2020-12-13T14:39:53.018Z"} Commented Dec 13, 2020 at 16:07

1 Answer 1

1

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

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

subscribedTo[0].infl

This will solve your issue for this case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.