简体   繁体   English

在 mongodb 中找不到嵌入文档

[英]cannot find embedded documents in mongodb

i am trying to find product reviews, based on their product id from products collection,我正在尝试根据产品集合中的产品 ID 查找产品评论,

Below is the code for finding that reviews that has id that matches product id, it is a nested object in reviewInfo object下面是查找具有与产品 id 匹配的 id 的评论的代码,它是 reviewInfo object 中的嵌套 object

const reviewid  = req.params.product_id
reviewSchema.find({reviewInfo:{reviewId:reviewid}}).then((value,err) => {console.log(value)})

i also have provided an image of my review schema我还提供了我的评论模式的图像

all i am getting is empty array.我得到的只是空数组。 image shows the reviewproduct schema structure图像显示 reviewproduct 架构结构

I think you are passing the wrong filter.我认为您通过了错误的过滤器。

You should use a filter,你应该使用过滤器,

{"reviewInfo.reviewId":reviewId}

so your code will be like,所以你的代码会像,

const reviewid  = req.params.product_id
reviewSchema.find({"reviewInfo.reviewId":reviewId}).then((value,err) => {console.log(value)})

in this query it just want to find an object exactly like this:在这个查询中,它只想找到一个 object,就像这样:

{reviewId:reviewid}

your query should be like this:您的查询应该是这样的:

const reviewid  = req.params.product_id reviewSchema.find({'reviewInfo.reviewId':reviewid}}).then((value,err)
=> {console.log(value)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM