2

我的架构:

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)

我的分页会中断。你能帮助我吗?

4

1 回答 1

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)
于 2020-06-01T12:54:25.900 回答