Features of MongoDB
These are some important features of MongoDB:
1. Support ad hoc queries
In MongoDB, you can search by field, range query and it also supports regular expression
searches.
2. Indexing
You can index any field in a document.
3. Replication
MongoDB supports Master Slave replication.
A master can perform Reads and Writes and a Slave copies data from the master and can only be
used for reads or back up (not writes)
4. Duplication of data
MongoDB can run over multiple servers. The data is duplicated to keep the system up and also
keep its running condition in case of hardware failure.
5. Load balancing
It has an automatic load balancing configuration because of data placed in shards.
6. Supports map reduce and aggregation tools.
7. Uses JavaScript instead of Procedures.
8. It is a schema-less database written in C++.
9. Provides high performance.
10. Stores files of any size easily without complicating your stack.
11. Easy to administer in the case of failures.
12. It also supports:
JSON data model with dynamic schemas
Auto-sharding for horizontal scalability
Built in replication for high availability
The architecture of MongoDB NoSQL Database
The following are the components of MongoDB architecture:
Database
Collection
Document
Database
It is also called the physical container for data. Every database has its own set of files existing on
the file system. In a single MongoDB server, there are multiple databases present.
Collection
The collection consists of various documents from different fields. All the collections reside
within one database. In collections no schemas are present.
Document
The set of key values are assigned to the document which is in turn associated with dynamic
schemas. The benefit of using these schemas is that a document may not have to possess the
same fields whereas they can have different data types.
MongoDB Create Database
Use Database method:
There is no create database command in MongoDB. Actually, MongoDB do not provide any
command to create database.
It may be look like a weird concept, if you are from traditional SQL background where you need
to create a database, table and insert values in the table manually.
Here, in MongoDB you don't need to create a database manually because MongoDB will create
it automatically when you save the value into the defined collection at first time.
to create database
If there is no existing database, the following command is used to create a new database.
Syntax:
use DATABASE_NAME
If the database already exists, it will return the existing database.
Let' take an example to demonstrate how a database is created in MongoDB
. In the following example, we are going to create a database "javatpointdb".
See this example
use javatpointdb
Swithched to db javatpointdb
To check the currently selected database, use the command db:
1. >db
javatpointdb
To check the database list, use the command show dbs:
>show dbs
local 0.078GB
Here, your created database "javatpointdb" is not present in the list, insert at least one
document into it to display database:
1. >db.movie.insert({"name":"javatpoint"})
WriteResult({ "nInserted": 1})
>show dbs
javatpointdb 0.078GB
local 0.078GB
MongoDB Create Collection
In MongoDB, db.createCollection(name, options) is used to create collection. But usually you
don?t need to create collection. MongoDB creates collection automatically when you insert some
documents. It will be explained later. First see how to create collection:
Syntax:
1. db.createCollection(name, options)
Here,
Name: is a string type, specifies the name of the collection to be created.
Options: is a document type, specifies the memory size and indexing of the collection. It is an
optional parameter.
Let's take an example to create collection. In this example, we are going to create a collection
name SSSIT.
1. >use test
switched to db test
1. >db.createCollection("SSSIT")
{ "ok" : 1 }
To check the created collection, use the command "show collections".
>show collections
SSSIT
MongoDB create collection automatically
MongoDB creates collections automatically when you insert some documents. For example:
Insert a document named seomount into a collection named SSSIT. The operation will create the
collection if the collection does not currently exist.
1. >db.SSSIT.insert({"name" : "seomount"})
2. >show collections
3. SSSIT
Syntax:
db.collection_name.find()