简体   繁体   English

mongoDb中使用mongoose子查询文档

[英]Sub querying documents using mongoose in mongoDb

I have 2 schemas:我有 2 个模式:

const shop = new mongoose.Schema({
id:String,
name:{type: String , required:true},
address:String,

reviews: [reviewSchema]
});

const reviewSchema = new mongoose.Schema({
    author: String,
    rating: {type: Number, required: true, min: 0, max: 5},
    reviewText: String,
    createdOn: {type: Date, "default": Date.now}

});

Using mongoose I want to query a specific review using review id in a particular shop.使用 mongoose 我想在特定商店中使用评论 ID 查询特定评论。 My api will be like this: http://localhost:3000/api/locations/:shopId/reviews/:reviewId I looked in the documentation but it is working with other paths such as author but not with id.我的 api 将是这样的: http://localhost:3000/api/locations/:shopId/reviews/:reviewId我查看了文档,但它正在使用其他路径,例如作者而不是 id。

const location = await Loc.findById(req.params.locationId)
const result = location.reviews.id(req.params.reviewId); 

This method gives the correct results using mongoose.此方法使用 mongoose 给出正确的结果。

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

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