-1

I am learning the MEAN framework. I don't understand why is the mongodb so popular that it is build in the framework and you cannot easily change the database (or can I?).

My questions are.

  • Why is the mongodb used in the MEAN framework?
  • Why is not there any choice between noSql and Sql database?
  • Is the MEAN framework (that uses mongodb) suitable only for some types of web projects or for any type of the web projects?
  • How can I create this example database in the mongodb?

If I have for example this relational database:

User
  id
  name

Car
  id
  user_id
  car_make_id      
  name

CarMake
  id
  Name

Than it is possible to easy make queries:

  1. Show me user and his cars. One query with join.
  2. Show me all cars in my systems, owners and their types. One query with join.

When I create this database in mongodb:

  • I lost the ability of real database transactions.
  • How should be the best implementation of this database in mongodb? I can use the same database structure, but it will be slower because I will have to make more requests into the database when I will create the queries above. Or the queries with the $lookup will be slower than real join in the sql database - am I right?

I can change the database structure like this

User
  id:
  name:
  cars: []

Car
  id:
  car_make_id:
  name:

CarMake
  id:
  Name:

Or I can change the database structure like this. The list of cars is embedded inside the user.

User
  id:
  name:
  cars:
  [
    {
      id:
      car_make_id:
      name:
    }
  ]

CarMake
  id:
  Name:

But how can I get: Show me all cars in my systems, owners and their types. Could you help me please to see the world in the mongodb way?

Myth Rush
  • 1,088
  • 1
  • 9
  • 19

1 Answers1

0

MEAN stack is basically use to create single page applications. And strongly focus on the pages(I mean the architecture of the application.)

1). Best practice of the mongoDb is to create Collections for the page. That means if one page uses several types of data. it is better if you can embed the documents in the collection(several types of data in in one collection. But in some cases we need to make references instead embeding).

2). Mongodb uses JSON to do transactions and it is easy to used in javascript frameworks.

3). Mongodb uses caching (it uses RAM to keep recent data in the memory) like javascript. So it is easy to do queries fast(Since less IO with hard disks.).

For your example you can use

User:{id:
      name:
      cars:
      [
        {
          id:
          car_make_id:
          name:
        }
      ]}       


 CarMake:{
       id:
      Name:}

Since you can index using id of the first collection. Since you can retrive all the data in one time. (If you embed the collection. all the data in the document is in the near blocks in the hard disk. So data retriving is fast)

Community
  • 1
  • 1
Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58
  • In this database structure. How can I do view with all cars in my system? I have to retrieve all users and read all embedded cars? – Myth Rush Apr 21 '16 at 09:47
  • No. You can use distinct command with 'Cars.name'. Or else you can use aggregation framework to do so. But when you design the database. First design the application and the database. (Reverse thing when we use relational databases.) – Lakmal Vithanage Apr 21 '16 at 09:52
  • please, consider to accept the answer if you got help. It will helpful other with the same kind of issue. – Lakmal Vithanage Dec 29 '16 at 06:37