MongoDB



MongoDB is an open source NoSQL database management program. NoSQL is used as an alternative to traditional relational databases. NoSQL databases are quite useful for working with large sets of distributed data. MongoDB is a tool that can manage document-oriented information, store or retrieve information.

Like any other database management language, MongoDB is based on a NoSQL database that is used for storing data in a key-value pair. Its working is based on the concept of document and collection. It is also an open-source, a document-oriented, cross-platform database system that is written using C++.


Mongo DB can be defined as a document-oriented database system that uses the concept of NoSQL. It also provides high availability, high performance, along with automatic scaling. This open-source product was developed by the company - 10gen in October 2007, and the company also maintains it. MongoDB exists under the General Public License (GPL) as a free database management tool as well as available under Commercial license as of the manufacturer. MongoDB was also intended to function with commodity servers. Companies of different sizes all over the world across all industries are using MongoDB as their database.

Advantages of MongoDB:

  • Flexible document schemas.
  • Code-native data access.
  • Change-friendly design.
  • Powerful querying and analytics.
  • Easy horizontal scale-out.



Disadvantages of MongoDB:

  • MongoDB uses high memory for data storage.
  • There is a limit for document size, i.e. 16mb.
  • There is no transaction support in MongoDB.

This section contains a guide for how to using MongoDB



Install MongoDB


Download the binaries from the MongoDB Download Center


1. Open Windows Explorer/File Explorer.

2. Change the directory path to where you downloaded the MongoDB .msi file. By default, this is %HOMEPATH%\Downloads.

3. Double-click the .msi file.

4. The Windows Installer guides you through the installation process. If you choose the Custom installation option, you may specify an installation directory. MongoDB does not have any other system dependencies. You can install and run MongoDB from any folder you choose.

Note


This tutorial assumes that you installed MongoDB in C:\Program Files\MongoDB\Server\4.2\.



Run MongoDB

First Step

Set up the MongoDB environment.

MongoDB requires a data directory to store all data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB. Create this folder by running the following command in a Command Prompt:

                            
                                md \data\db
                            
                            

You can specify an alternate path for data files using the --dbpath option to mongod.exe , for example:

                                    
                                        "C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath d:\test\mongodb\data
                                    
                                

If your path includes spaces, enclose the entire path in double quotes, for example:

                            
                            "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --dbpath "d:\test\mongo db data"
                            
                            

You may also specify the dbpath in a configuration file.

Second Step

Start MongoDB.

To start MongoDB, run mongod.exe. For example, from the Command Prompt:

                            
                            "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"
                            
                            

This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.




Depending on the security level of your system, Windows may pop up a Security Alert dialog box about blocking “some features” of C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe from communicating on networks. All users should select Private Networks, such as my home or work network and click Allow access. For additional information on security and MongoDB, please see the Security Documentation.

Third Step

Verify that MongoDB has started successfully

Verify that MongoDB has started successfully by checking the process output for the following line:

                            
                            [initandlisten] waiting for connections on port 27017
                            
                            

The output should be visible in the terminal or shell window.


You may see non-critical warnings in the process output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.

Fourth Step

Connect to MongoDB.

To connect to MongoDB through the ~bin.mongo.exe shell, open another Command Prompt.

                            
                            "C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"
                            
                            

PyMongo

PyMongo is a Python module used to interface between MongoDB and Python applications. To install the PyMongo library, install onto your version of Python from command line using the following command:

                
                    
                        
                        pip install pymongo
                    
                
            

To import the PyMongo library into your scripts, use the following code to have access to it as a reference:

                
                    
                
            

You'll generally want to use the MongoClient object to gain access to the mongod instance while it is running.

                
                    
                
            

You can access a particular database using the MongoClient object. From there, you can also search for particular collections(tables) within the database as well.

                
                    
                
            

MongoDB uses documents, written in BSON, which act as database records. You can insert either one document or multiple at a time. These can also be queried, updated, deleted, much like any other CRUD operations in a typical database.

                
                    
                
            

Source: Tutorial - PyMongo