Structuring mongo db vs mysql

Black Sulfur
Black Sulfur used Ask the Experts™
on
I have only worked with php and mysql so am used to normalising my database and doing a lot of table joins etc. but I am moving over to node.js now and using mongoose with mongo DB and what I have gathered is that you shouldn't structure your db in the same way you would using php mysql. Apparently you should rather have a denormalised database. Could anyone give me an idea of where I could find a good example of this or possibly explain this in terms of an example? I have attached an image of a relational database setup I found on google and just wondered how the mongo db database would differ?
relational.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Developer
Commented:
this gives a good explanation:
https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/

The main difference would be that the orders table would have orders detail as a field rather than a separate table (probably as a nested document).

If you really want to learn they offer free courses:
https://university.mongodb.com/courses/M001/about
ste5anSenior Developer
Commented:
Apparently you should rather have a denormalised database.
hmm, nope. The problem are the concepts.

Normalization is defined in for relational database systems as how to implement tables to avoid redundancy and to enforce logical integrity. Sadly enough, they reuse the word, but change the meaning. Here "Normalization" means relationship between documents.

So, as MongoDB is NoSQL and document oriented, apply this to your ERD: You simply have 1..N product descriptions and 1.N orders in your database. When an order is placed, you simply store the entire order as a single document in your database. E.g. something like

{
    _id: <ObjectId1>,
    orderNo: 1232,
    invoiceAddress: { /* complete address */ },
    shippingAddress: { /* complete address */ },
    orders: [ { /* complete order position */ }, { /* complete order position */ } ],
    orderTotalAmonut: 0.0,
    orderTax: 0.0
}

Open in new window

nociSoftware Engineer
Distinguished Expert 2018
Commented:
They both have their strong points.

Consider looking up an order position , you will need to search all Orders to find it in NoSql.
In SQL you could would have a different table with it's own indexes.

Can't find a  link any more.. Someone converted a Mysql -> MongoDB for a movie database.
No problem to order using films as a starting point and drill down to cast  etc.
Finding an actor and in what films they played was quite not that simple to build in anymore.
(more or less the same problem).  N:M relations can be a problem.
ste5anSenior Developer

Commented:
You query Mongo by using "filter documents".

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial