一个简单的问题,想知道MongoDB中的最佳解决方案:
在SQL(RDBMS)环境中,我们有两个表Customers和Products 所以对于这个报告我们可以简单地写出正确的查询 报告:去年十大客户已经购买了索尼产品 QUERY:
SELECT c.name,sum(price) total FROM customers c JOIN orderItems o ON(c.cid=o.cid)
JOIN products p ON(p.pid=o.pid)
WHERE p.manufacture='SONY' AND o.date=2014
GROUP BY c.name
ORDER BY DESC total
MongoDB的最佳方法是什么?
答案 0 :(得分:1)
我认为这样的最佳方法是将所有必要的数据直接保存到客户集合中,其结构类似于:
{
_id: <ObjectId1>,
"customer_name": "XYZ",
"orders": [
{
"id": 12345,
"date": "2014",
"items": [
{
"id": 1,
"manufacturer": "SONY",
"product": "WALKMAN NWZ-W273L",
"color": "blue"
},
{
"id": 2,
"manufacturer": "SONY",
"product": "VAIO XYZ",
"color": "silver"
}
]
}
]
}