首页 > 解决方案 > 使用单个请求获取相同密钥的有限数量文档

问题描述

我在 mongodb 中有以下模型。数据库有大约 1000 条记录,如果可能的话,我想用一个请求为每个不同的 storeName 只获取 10 个文档。我不想为每个 storeName 提出请求。例如 storeName1 = Offers.find({storeName: "storeName1"}).limit(10), storeName2 = Offers.find({storeName: "storeName2"}).limit(10)

const mongoose = require("mongoose");

const offersSchema = new mongoose.Schema({
    imgLink: String,
    title: String,
    startPrice: Number,
    discountPrice: Number,
    pageLink: String,
    storeName: String,
    offerType: String,
    discountPercentage: {
        type: String,
        trim: true
    }
});

module.exports = mongoose.model("Offers", offersSchema);

标签: mongodbmongoose

解决方案


推荐阅读