我想实现的是过滤所有订单,得到每个卖家销售的所有产品


const OrderSchema = new Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
orderDate: { type: Date, default: Date.now },
products: { type: Array, default: [] }
});
const Order = mongoose.model("Order", OrderSchema);
module.exports = Order;
"products" : [
{
_id: "5f0289147b9b980f40d4f2e6",
product: ObjectId("5f02879f7160ae0c2c203cdf"),
quantity: 1
},
{
_id: "5f0289177b9b980f40d4f2e7",
product: ObjectId("5f02879f7160ae0c2c203fgg"),
quantity: 1
}
]
"product" : {
_id: "5f02879f7160ae0c2c203cdf",
name: "Some Car",
category: "5f027c5ca1b94820b856c508",
seller: "5f0276cf965f8c29e019a7f1" //seller can be different for each product
}

我想实现的是过滤所有订单,以获得每个卖家销售的所有产品。就像如果我是一个卖家,并且我登录了,我想在订单集合中获得我销售的所有产品

您应该在表之间有一个共享的id,这样您就可以const soldProducts = products.filter(product => product.id === id)soldProducts将是一个新的产品数组,它具有与您的用户id相匹配的id(如果您的数据库就是这样构建的(。我认为这将是最简单的方法。

最新更新