0
是否可以在Mongodb中聚合嵌套數組元素本身?例如原始數據是在MongoDB中聚合嵌套數組元素本身
{"transId" : "12345","customer" : "cust1", "product" : [{"type" : "cloth","price" : 100},{"type" : "toy","price" : 200}]}
{"transId" : "45672","customer" : "cust1", "product" : [{"type" : "cloth","price" : 10},{"type" : "toy","price" : 500}]}
{"transId" : "99999","customer" : "cust2", "product" : [{"type" : "cloth","price" : 40},{"type" : "toy","price" : 5}]}
我希望每個嵌套數組元素根據客戶的類型進行聚合,例如,
結果:
{"customer" : "cust1", "product" : [{"type" : "cloth","price" : 110},{"type" : "toy","price" : 700}]}
{"customer" : "cust2", "product" : [{"type" : "cloth","price" : 40},{"type" : "toy","price" : 5}]}
能否請你幫告訴我該怎麼做?謝謝。
一個加法。將'{$ project:{_ id:0,customer:「$ _ id」,product:1}}'追加到聚合管道以符合OP所需的格式。 – Saleem
@Saleem不!增加'$ project'會導致性能下降。 – styvane