我有以下架構:
const RecipeSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
ingredients: [
{
name: {
type: String,
required: true,
trim: true
},
amount: {
type: Number,
required: true
}
}
]
});
const Recipe = mongoose.model('Recipe',RecipeSchema);
export {Recipe as default}
我堅持通過提供兩個數字來過濾食譜:min 和 max,并回傳成分數(成分陣列的長度)大于 min 且小于 max 的食譜串列。你能幫我解決這個問題嗎?
uj5u.com熱心網友回復:
在這里,我使用 $expr 運算子過濾掉所有大小大于或等于 min 且小于或等于 max 的檔案。
let min = 1
let max = 2
RecipeModel.find({"$expr":{$gte:[{$size:"$ingredients"},min], $lte:[{$size:"$ingredients"},max]}})
uj5u.com熱心網友回復:
這個序列終于做到了:
{$where: "this.ingredients.length >= " min " && this.ingredients.length <= " max}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/374674.html
