Nav
Nav

Reputation: 4540

Data Modelling with Mongo

A simple question and wondering the best solution in MongoDB:

IN a SQL(RDBMS) environment we have two tables Customers and Products So for this report we could simply write proper query: REPORT: Give top ten customer have bought Sony products in last year 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

What is the best approach for MongoDB?

Upvotes: 0

Views: 63

Answers (1)

Lukas Hajdu
Lukas Hajdu

Reputation: 804

I thing that the best approach for something like this is to save all necessary data straight to customer collection with structure similar to this:

{
    _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"
                }
            ]
        }
    ]
}

Upvotes: 1

Related Questions