Installing and data modeling in MongoDB | Download  the  MongoDB based on your system architecture whether it is “32 bit or 64 bit” for windows. You can check the version of your system in the control panel. After successfully downloading the file, extract and save it on C drive. Create two folders data and inside data db. Wants to Learn MongoDB from scratch- Basics

Note: If you are not saving files in C drive, then make sure to type the correct path while creating the new folders.

C:\>md data
C:\md data\db

To run in Windows:

C:\>cd "set up"
C:\set up>cd mongodb
C:\set up\mongodb>cd bin
C:\Program Files\MongoDB\Server\3.4\bin>mongo.exe

NOTE: Here we assume, you have already installed your setup in the above location by default.

Ubuntu

you can install MongoDB by typing the command in command prompt.

apt-get install mongodb-10gen = 2.2.3

When you are done with the installation, start the MongoDB. Write the following command for the Ubuntu system:

sudo service mongodb start // to start the services
sudo service mongodb stop // to stop the services

You can know the list of commands in MongoDB, just for the knowledge by typing db.help():

        > db.help()                                                                                                                                                   
        db.getReplicationInfo()                                                                                                                                
        db.getSiblingDB(name) get the db at the same server as this one                                                                                        
        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set                               
        db.hostInfo() get details about the server's host                                                                                                      
        db.isMaster() check replica primary status                                                                                                             
        db.killOp(opid) kills the current operation in the db                                                                                                  
        db.listCommands() lists all the db commands                                                                                                            
        db.loadServerScripts() loads all the scripts in db.system.js                                                                                           
        db.logout()                                                                                                                                            
        db.printCollectionStats()                                                                                                                              
        db.printReplicationInfo()                                                                                                                              
        db.printShardingStatus()                                                                                                                               
        db.printSlaveReplicationInfo()                                                                                                                         
        db.dropUser(username)                                                                                                                                  
        db.repairDatabase()                                                                                                                                    
        db.resetError()                                                                                                                                        
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }                                                     
        db.serverStatus()                                                                                                                                      
        db.setLogLevel(level,<component>)                                                                                                                      
        db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all                                                                                                
        db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db                                                                
        db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db                                                            
        db.setVerboseShell(flag) display extra information in shell output                                                                                     
        db.shutdownServer()                                                                                                                                    
        db.stats()                                                                                                                                             
        db.version() current version of the server

db.stats() will show you that name of the database, number of collections in the database and the objects and the indexes if any assigned in MongoDB.

> db.stats()                                                                                                                                                  
{                                                                                                                                                             
        "db" : "test",                                                                                                                                        
        "collections" : 0,                                                                                                                                    
        "objects" : 0,                                                                                                                                        
        "avgObjSize" : 0,                                                                                                                                     
        "dataSize" : 0,                                                                                                                                       
        "storageSize" : 0,                                                                                                                                    
        "numExtents" : 0,                                                                                                                                     
        "indexes" : 0,                                                                                                                                        
        "indexSize" : 0,                                                                                                                                      
        "fileSize" : 0,                                                                                                                                       
        "ok" : 1                                                                                                                                              
}                                                                                                                                                             

In this tutorial, we will be looking at the data model of MongoDB and some of its examples.

What is data model?

It is the first step in creating the object oriented programming and deign how to relate each item to each other and changes the conceptual model to the logical model and finally to the physical schema. It is a single document structure.

Here are some rules for designing data model:

  • Keep in mind what the user wants and their requirements.
  • You are free to use the complex aggregation in the schema.
  • Keep all the objects in the one document if you require them together to reduce the join operation.
  •  You can use the join operation while writing but it is not allowed while reading from the queries.
  • Data can be duplicated and stored because the disk space is less expensive but is limited to some extents.
  • Finally, the schema should be optimized according to the use cases.

The difference between the data models of MongoDB and relational database are:

In MongoDB, there is one common Id to access the documents whereas, in a relational database, we have to assign Id to each table and have to declare it as the primary key. There will be complex join operation in the relational database whereas, in MongoDB, there is no complex join.

Designing the model of MongoDB is much easy as compared to the relational database.

Example:

All the documents will be in a single collection.

Suppose the database administrator wants to save the all the names of the hosting sites who are taking one or more tutorials. It can be saved in the document so that the data can be easily retrieved.

First of all, we will list out the requirements that we need in the documents such as Id for key and value pair, title, description, courses, price. We will keep all these fields in the same document to avoid any complex joins. The schema optimization is optional to use.

The data model for MongoDB will be:

{
  id: "123456789",
  title: "Mongodb tutorial",
  description: "MongoDB tutorials from mytrendin.com",
  new_courses: {
    id_: "234597",
    other_courses_name: "introduction to html from beginner to advance level with my trend in",
    tutor: "xyz"
  },
 
  price: {
    fees: 1000
  }
}

If You are Beginner  Learn MongoDB from the scratch

If you really liked the article, please subscribe to our YouTube Channel for videos related to this article.Please find us on Twitter and Facebook.

Related Posts

Categories: