afnan
afnan

Reputation: 13

cannot find embedded documents in mongodb

i am trying to find product reviews , based on their product id from products collection,

Below is the code for finding that reviews that has id that matches product id , it is a nested object in reviewInfo 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

Upvotes: 0

Views: 229

Answers (2)

Archana Agivale
Archana Agivale

Reputation: 405

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)})

Upvotes: 3

Badri Derakhshan
Badri Derakhshan

Reputation: 61

in this query it just want to find an object exactly like this :

{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)})

Upvotes: 1

Related Questions