Features:

->MongoDB is a No SQL database. It is an open-source, cross-platform, document-oriented database written in C++.C-> It stores data as documents, so it is known as document oriented database.

-> documents are separated by “.”

NoSQL Database:

-> It doesn’t use tables for storing data.

-> It is generally used to store big data and real-time web applications.

Advantages:

-> No schema -one collection holds different documents.

-> There may be difference between number of fields, content and size of the document from one to other.

-> No complex joins

-> Facility of deep query

-> It is very esay to upgrade and light weight.

-> It uses internal memory for storing working sets and this is the reason of its fast access than RDBMS.

Comparaison with RDBMS:

-> Collection is used like a table in mangodb.

-> MongoDB is almost 100 times faster.

-> MongoDB is a document oriented database in which data is written in BSON format     which is a JSON like format not like multiple schema and in each schema we create tables to store data in RDBMS.

Data Types:

Min/Max Keys: This datatype compare a value against the lowest and highest bson elements

MongoDB Shell :

-> This is a JavaScript shell that allows interaction with MongoDB instance from the command line.

-> If we want to create a table, we should name the table and define its column and each column’s data type.

Note: This is an editor in mango DB  like an sql editor in RDBMS.

Create Database:

-> There is no create database command in MongoDB.

->  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.

cmd: use DATABASE_NAME 

Note: If the database already exists, it will return the existing database. Otherwise creates new database with the given name.

Check the currently selected database:

cmd: db  

Check the database list:

cmd:show dbs

Note: To see the database in the list it must have atleast single document in it.

DropDatabase:

The dropDatabase command is used to drop a database. It also deletes the associated data files. It operates on the current database.

cmd: db.dropDatabase()  

Note: In the case we have not selected any database, it will delete default “test” database.

-> To delete the database sampledb first  go to the database using the cmd

cmd: use sampledb

-> Then use the below cmd to drop the database.

cmd: db.dropDatabase()  

CreateCollection:

MongoDB creates collection automatically when you insert some documents. If we want to create it manually we can use the cmd.

Syntax: db.createCollection(name, options)   

Name: It is a string type, specifies the name of the collection to be created.

Options: It  is a document type, specifies the memory size and indexing of the collection. It is an optional parameter.

Eg:db.createCollection(“Employee”)  

To check the created collection:

cmd: show collections  

MongoDB create collection automatically:

Insert a document named John into a collection named Employee. The operation will create the collection if the collection does not currently exist.

cmd:db.Employee.insert({“name” : “John”})

To see the inserted document:

cmd:db.collection_name.find()

To drop a collection:

It completely removes a collection from the database and does not leave any indexes associated with the dropped collections.

Synatx: db.COLLECTION_NAME.drop()  

Eg:db.Employee.drop()  

Note: The drop command returns true if it successfully drops a collection. It returns false when there is no existing collection to drop.

Insert documents:

It is used to add or insert new documents into a collection in your database.

Syntax:db.COLLECTION_NAME.insert(document)  

Eg: db.sampledb.insert(

{

course: “java”,

details: {

duration: “6 months”,

Trainer: “Sonoo jaiswal”

},

Batch: [ { size“Small”, qty: 15 }, { size“Medium”, qty: 25 } ],

category: “Programming language”

}

)

Note: There are another two ways to insert the documents into the collection using arrays and bulk insert.

 Upsert:
-> Upsert is an operation that performs either an update of existing document or an insert of new document if the document to modify does not exist.
-> “db.collection.update()”  and “db.collection.save()” methods add new documents through an operation called upsert.
Update:update() method is used to update or modify the existing documents of a collection.
Syntax:db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)  

-> To update the existing course “java” into “android”

cmd:db.Employee.update({‘course’:‘java’},{$set:{‘course’:‘android’}})  

To delete documents from a collection:

Syntax:db.collection_name.remove (DELETION_CRITERIA)

Eg:db.Employee.remove({})

-> The above cmd is used to remove all the documents from the collection. It does not remove indexes.

Remove all documents that match a condition:

cmd:db.Employee.remove( { type : “programming language” } )  

-> It will remove all documents from the Employee collection where the type field is equal to programming language.

Remove a single document that match a condition:

-> It will remove a single document from the Employee collection where the type field is equal to programming language.

cmd:db.Employee.remove( { type : “programming language” }, 1 )  

Retrieve documents from a collection:

Syntax:

db.COLLECTION_NAME.find({})

or

db.COLLECTION_NAME.find()

limit():

We have a lot of fields in collection of our database and have to retrieve only 1 or 2. In such case, limit() method is used.

cmd:db.COLLECTION_NAME.find().limit(NUMBER)   

skip():

This method is used to skip the document. It is used with find() and limit() methods.

Syntax:db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)   

Eg:db.Employee.find().limit(1).skip(2)

-> Here the skip() method has skipped first and second documents and shows only third document.

sort() :

This method is used to sort the documents in the collection

  • 1 is used for ascending order sorting.
  • -1 is used for descending order sorting.

Syntax:db.COLLECTION_NAME.find().sort({KEY:1})  

Eg:db.Employee.find().sort({“Course”:-1})