[英]MongoDB query nested array knowing parent id element (JavaScript)
If I have a MongoDB collection that looks like this:如果我有一个看起来像这样的 MongoDB 集合:
_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?有或没有 ZCCADCDEDB567ABAE643E15DCF0974E503Z,我如何查询 object subscribedTo
的第一个孩子,称为infl
,它是一个数组?
what I'm trying is this, but it says "infl" is undefined:我正在尝试的是这个,但它说“infl”是未定义的:
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:虽然当我控制台日志只foundUser.subscribedTo
它正确记录:
this [{"infl":["item1","item2"],"inflId":["item1id","item2id"]}]
According to your code output, subscribedTo
is not an object
,根据您的代码 output, subscribedTo
不是object
,
it's an array of objects.它是一个对象数组。 So, what you need is:所以,你需要的是:
subscribedTo[0].infl
This will solve your issue for this case.这将解决您在这种情况下的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.