MongoDB Shell

MongoDB Shell

The interactive java-script Mongodb shell accesses MongoDB. It can also be accessed within the programming environment. You can also use a MongoDB driver of your programming environment to connect to a running MongoDB instance. The MongoDB shell includes the MongoDB package. This shell is used to perform various operations and administrative functions.

Start with the shell- 

  1. To run MongoDB shell, open command prompt window and enter mongo.exe.(for windows). Make sure that in the other command prompt window, mongod.exe process is open other than the new one.
Start with the shell
(Execution of mongo.exe on the command prompt)

2. The MongoDB website also has a list of drivers for various languages like C, C++, Python, Perl, Ruby, Java, Node.js, Scala, PHP, etc. that can also be used for the connection.

After establishing the connection with the MongoDB process, you are ready to create and work on your database.

Execution of mongod.exe on the command prompt
(Execution of mongod.exe on the command prompt)

The shell is fully capable of running an Arbitrary Javascript program as it has a JavaScript interpreter.

MongoDB Data Modeling

As we know that MongoDB works on the document-oriented database, it deals in fields, collection, and documents. The single collection allows storing different sets of fields. In a collection, common fields can contain different types of data that gives ease in mapping.

The biggest challenge in MongoDB data modeling is balancing the requirement of the application, identify the patterns for data retrieval, & to ensure the performance of the database engine. Designing of the data model depends on the usage of the application. 

Document Structure for MongoDB

The structure of the document entirely depends on the relationship among data. In any application, there are two ways to represent the relationship between data, and you can use either references or Embedded data tool.

References: This tool works on the concept of the normalized data model that stores the data by including a link from one data to the other data. The reference tool is more flexible than embedded. It is applicable to represent hierarchical data set & complex many to many, one to many relationships with document references.

Structure of the Reference Model
(Structure of the Reference Model)

Embedded Data Model

This model work on the concept of de-normalized data models, which embed related data in a single document. The embedded data model is useful to represent one to one or one to many relationships with embedded documents. 

Embedding provides better performance for read operations & with the help of a single write query, it updates all related data.

 -id: ,
 Username: “Javatpoint”,
 contact: {    
            Phone: “999999999” ,
            Email: “[email protected]”
 access: {
      level: 7,
      group: “Noida”

       (Embedded Document Structure)

Essential tips for designing the schema in MongoDB

While we design a MongoDB data model, some points must be considered for high performance and efficiency of database, which are given below:

1. Data pattern: 

  1. You must be aware of how the particular application is going to access the database before designing the model for it. 
  2. Also, the pattern of data in terms of reading, writing, updating, and deletion must be known before designing.
  3. The frequency of usage of data should also be considered.
  4. Schema designing is based on user requirements.

2. Document Size:

In MongoDB, during initialization, documents are assigned of the fixed size, but after some updation, their size tends to increase. So, while using the embedded document, one must analyze whether the sub-object will grow and cross the size limit, which otherwise will cause performance degradation. MongoDB does the relocation of the document on the disk if the document size increases.

3. Atomicity:

Atomicity here results in either the success or failure of a group of operations as a single unit. The parent operation gets success if all the sub-operations execute successfully. Parent transactions will fail if any single operation is not ready to commit. MongoDB based on document structure so that a single write operation can affect only one collection. We can insert or update data in any entity with the help of a single write operation.  

By using the above tips, we came to know how MongoDB data model works without degrading its performance & the difference between reference & embedded document structure.