我的模式:
const ProductSchema = new Schema({
Category: String,
Subcategory: String,
Name: String,
Offers: [{ type: ObjectId, ref: "Offer" }]
})
const OfferSchema = new Schema({
Quantity: Number,
Images: [String],
Price: Number,
Size: String,
Color: String
})
我正在查询带有限制和跳过过滤条件的产品。我尝试过:
const products = await ProductSchema.find({ ...someFilter }).populate({
path: "Offers",
match: {
Quantity: { $gt: 2 },
Images: { $exists: true, $ne: [] }
}
}).skip(skip).limit(limit)
我只想获取要约长度> 0的文档。但是我要获取要约空的文档。如果我这样过滤:
products.filter(item => item.Offers.length > 0)
我的分页将中断。你能帮助我吗?
答案 0 :(得分:0)
只需要求Offers
字段不能为空,就像这样:
const products = await ProductSchema.find(
{
...someFilter,
"Offers.0": {$exists: true}
}
).populate({
path: "Offers",
match: {
Quantity: {$gt: 2},
Images: {$exists: true, $ne: []}
}
}).skip(skip).limit(limit)