MongoDB is a document-oriented database used to store and process data in the form of documents.MongoDB provides some basics but essential operations that we can easily interact with MongoDB servers. The basics of operation are 

Document: Documents are basically just an object nothing else, like a row in SQL based database system.

{
  regNo: "3014",
  name: "Test Student",
  course: {
    courseName: "MCA",
    duration: "3 Years"
  }
}

Collection: Collections in MongoDB likes tables in the SQL database. They are collections of documents. If a collection doesn't exist in the database, then it will create automatically a new collection in the database.


1. Create

This involves writing / inserting data into the database. MongoDB provides the following methods to insert documents into a collection: In this case the document we will insert a simple JSON object

  1. db.collection_name.insertOne(document)
  2. db.collection_name.insertMany(document)
after successfully insert it returns
{
"acknowledge": true
}

2. Read:

 which query used  to retrieve data from a database

  1. db.collection_name.find()

That find( ) method has 2 optional parameters.

find(query, projection)

db.collection_name.find({"fieldname":"value"})

3. Update: 

which change data that already exists in a database. You can update the documents in your database with following commands.

  1. db.collection_name.updateOne( )
  2. db.collection_name.updateMany( )

With parameters:

db.collection.updateOne(filter, update)

db.collection_name.updateOne({_id: 1}, {$set: {number: 5}})


First parameter is the filter parameter that we would like to update documents which id no 2. Then we create a new field using {$set: { field: value}} 

4. Delete: which permanently remove data from a database. These operations allow us to remove documents from a collection. As usual, we have 2 methods

  1. db.collection.deleteOne( )
  2. db.collection.deleteMany( )
db.collection_name.deleteOne({_id: 1})