- MongoDB is a document store.
- Documents in MongoDB are organized in BSON format. BSON is the short of Binary JSON. BSON is the binary counterpart of JSON.
- MongoDB is a NoSQL data store.
- Generally the RDBMSes like MySQL Server use a defined schema and the data is populated from various sources like Application Programs as per the schema definition.
- MongoDB does not store schema separately as it uses the binary JSON format, which has both schema and data stored together.
- In a conventional RDBMS ,if the data is to be migrated from one schema to another schema then the schema has to be modified first followed by the data. This kind of migration cannot guarantee reduced downtime of the server without using any backup server and rerouting.
- In MongoDB the data migration can be less costly even on a single server deployment. While the server is online, a MongoDB collection can be modified of its documents one by one or in batches. This kind of migration results in zero downtime. This removes data migration as one of the factors for server redundancy.
- In MongoDB, every document can be considered as a record. 'n' number of documents form a MongoDB collection which is the RDBMS table equivalent. Several collections exist in a MongoDB database. Many databases exist in a MongoDB database server.
- For development purposes, the MongoDB Community Server can be downloaded for the appropriate platform from the MongoDB website and installed locally.
- For example, for Mac OS the installation process is just downloading the compressed file and then decompressing it into a folder.
Running the MongoDB server:
- mongod – is the MongoDB server process.
- After installing the MongoDB server create the folder /data/db by typing
mkdir -p /data/db
- MongoDB can be started by running the mongod process from the command line by typing the following:
Creating a database using Mongo Shell:
- mongo – Is the command line shell for the MongoDB and is called Mongo Shell. MongoDB can be accessed and database operations can be performed using Mongo Shell.
- To create a database using Mongo Shell, issue the use command with the name of the database that is to be created. This will create a database if the specified database is not present and load it.
- Issuing the command below in the Mongo Shell will create a Mongo database with the name sample.
Creating a collection using Mongo Shell:
- To create a collection under a MongoDB database use the Mongo Shell and issue the following command:
- The above command will create a collection if the collection does not exist and insert the document as per the JSON string specified.
Querying a collection using Mongo Shell:
- To query a collection from a MongoDB database use the find()command from the MongoDB shell.
- The following command will query and list the documents from a collection named test under a database called sample.
Installing PyMongo and accessing PyMongo from a Python Program:
- Once the MongoDB server is installed, started and few test databases and test collections are created the MongoDB server can be connected to from any Python program.
- PyMongo is the python driver for MongoDB.
- PyMongo can be installed using pip.
pip install pymongo
- Subsequent to installing PyMongo, the pymongo module can be imported inside a Python program to query a MongoDB collection.