[英]Sub querying documents using mongoose in mongoDb
我有 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}
});
使用 mongoose 我想在特定商店中使用評論 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);
此方法使用 mongoose 給出正確的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.