我想獲得包含所有打折產品的目錄。Mongodb:彙總數據並在每個元素上應用函數
2個文件:
/* Catalog schema */
{
Catalog: {
products: [{
type: ObjectId,
ref: 'Product'
}],
discount: {
global: { type: Number, default: 0 },
categories: [{
codeCategory: String,
discount: Number
}]
}
}
/* Product schema */
{
reference: {
type: String,
index: true,
unique: true
},
url_photo: {
type: String,
default: 'http://localhost:3001/app/img/no-picture.jpg'
},
category: {
type: ObjectId,
ref: 'Category'
},
short_description: String,
long_description: String,
price: Number,
information: String
}
的折扣適用於從目錄中的每個產品,我怎麼能做出蒙戈查詢目錄中得到所有產品與應用的折扣?
我可以直接查詢有這個結果嗎?
我已經檢查了聚合,但我是新的,我看不到我怎麼做到這一點。
謝謝
凡存儲的文件?在同一個集合中?您使用了哪些驅動程序,例如貓鼬,pymongo等?如果我不得不猜測,我會說你正在用貓鼬做一個Node應用程序。 – dasdachs
目錄,產品和類別是分離的集合。我正在使用貓鼬。你猜對了,它是節點應用程序。 – stephane
爲什麼在Catalog Schema中有兩個'discounts'? – dasdachs