我要创建一个电子商务网站。查询遇到了一些问题。举个例子,我有一个类别名称是“ computer”。 在产品架构内部使用计算机作为参考。我需要获得100个计算机类别的产品。我使用mongodb聚合来获取此查询。当我与品牌类别分组时,我只有_id,但是我需要获得类别名称。
const productSchema = mongoose.Schema(
{
name: {
required: true,
type: String,
unique: 1,
maxlength: 100
},
description: {
required: true,
type: String,
maxlength: 100000
},
price: {
required: true,
type: Number,
maxlength: 255
},
category: {
type: Schema.Types.ObjectId,
ref: "category",
required: true
},
);
productSchema.statics.categorylist = function() {
return this.aggregate([
{ $group: { _id: "$brand", count: { $sum: 1 } } }
]);
};
const Product = mongoose.model("product", productSchema);
output:
_id:5c4bf084985d642539a1e648,
count:14
but I need to get like this
_id:5c4bf084985d642539a1e648,
name:pc,
count:14
有什么方法可以填充类别名称? 我如何获得此查询?