Google News
logo
MongoDB Interview Questions
MongoDB is a cross-platform document-based database. Categorized as a NoSQL database, MongoDB avoids the conventional table-oriented relational database structure in support of the JSON-like documents with the dynamic schemas, making the data integration in specific kinds of applications quicker and simpler.
 
MongoDB was developed by a software company “10gen”, in October 2007 as an element of the planned platform as the service product. After that, the company was shifted to a freeware deployment model in 2009, providing sales assistance and other services.
Some advantages of MongoDB are as follows :
 
* MongoDB supports field, range-based, string pattern matching type queries. for searching the data in the database 
* MongoDB support primary and secondary index on any fields
* MongoDB basically uses JavaScript objects in place of procedures
* MongoDB uses a dynamic database schema
* MongoDB is very easy to scale up or down
* MongoDB has inbuilt support for data partitioning (Sharding).
A Document in MongoDB is an ordered set of keys with associated values. It is represented by a map, hash, or dictionary. In JavaScript, documents are represented as objects :

{"greeting" : "Hello world!"}
 
Complex documents will contain multiple key/value pairs :

{"greeting" : "Hello world!", "views" : 3}

A collection in MongoDB is a group of documents. If a document is the MongoDB analog of a row in a relational database, then a collection can be thought of as the analog to a table.

Documents within a single collection can have any number of different “shapes.”, i.e. collections have dynamic schemas.
 
For example, both of the following documents could be stored in a single collection:
 
{"greeting" : "Hello world!", "views": 3}
{"signoff": "Good bye"}

MongoDB stores BSON (Binary Interchange and Structure Object Notation) objects in the collection. The concatenation of the collection name and database name is called a namespace.
The procedure of storing data records across multiple machines is referred as Sharding. It is a MongoDB approach to meet the demands of data growth. It is the horizontal partition of data in a database or search engine. Each partition is referred as shard or database shard.
Across multiple servers, the process of synchronizing data is known as replication. It provides redundancy and increase data availability with multiple copies of data on different database server. Replication helps in protecting the database from the loss of a single server.
Points need to be taken in consideration are
 
* Design your schema according to user requirements
* Combine objects into one document if you use them together. Otherwise, separate them
* Do joins while write, and not when it is on read
* For most frequent use cases optimize your schema
* Do complex aggregation in the schema
A NoSQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases (like SQL, Oracle, etc.).
 
Types of NoSQL databases :
 
* Graph
* Document Oriented
* Key Value
* Column Oriented
MongoDB is a document oriented database. It stores data in the form of BSON structure based documents. These documents are stored in a collection.