Sunday, 17 December 2017

Part:1 CRUD Operations in MongoDB

Here, I am trying to explain CRUD operations with examples for better understanding.

So I am posting this in 2 parts. 

Part: 1: 


MongoDB has 4 basic operations Create/insert, Read, Update and Delete to add, read, modify and delete data in the shell.

CREATE:


This operation is used to add (create or insert) documents to a collection. 

We have different methods to insert documents into a collection. If collection doesn’t exist, insert will create collection with given name. 

1. Inserts single or multiple documents into a collection.

     Syntax: 

           db.<<collection_name>>.insert({<<document>>)

        Ex:
  
          > db.sample.insert({ no:1, name: "aaa"})
          > db.sample.insert([ 
           { no:3, name: "ddd", add: "abc" },
     { no:4, name: "eee", pin: 12345 },
     { no:5, name: "fff" }
           ])  

2. Inserts single document into a collection.

     Syntax:
 
           db.<<collection_name>>.insertOne({<<document>>})

Note: This method returns a document with:  acknowledged with Boolean value(True/false) and insertedId with objectId(_id).

         Ex: 

         > db.sample.insertOne({ no:12, name: "abc", item: "aaa" }

3. This method inserts multiple documents into a collection.

      Syntax:

            db.collection.insertMany() 

Note: This method returns a documents with: acknowledged with Boolean value(True/false) and an array of objectId (_id) for successful inserted documents

          Ex:

       > db.sample.insertMany([
        { no:3, name: "ddd", add: "abc" },
        { no:4, name: "eee", pin: 12345 },
        { no:5, name: "fff" }
         ])

Note:  Both methods InsertOne() and InsertMany() are not compatible with db.collection.explain()
We can also insert documents by using variable.

          Ex:
   
      > var a = { no:10, name: "abc", add: "aaa"}
      > db.sample.insert(a)

READ:


This operation is used to read/retrieve/find existing documents from collection.

We have 2 methods to find documents from collection.

1. It returns all documents or documents which satisfies the given condition.

     Syntax:
 
          db.<<collection_name>>.find({<<query>>},{<< projection>>})
      Here, 

        Query:  It specifies filter condition (Like where in RDBMS) using query operators to return documents based on condition. (Query filter or criteria).

        Projection:  It specifies which fields are required to return in result.  (1 or true to include field, 0 or false to exclude field).
Note: Both (query and projection) are optional. If not specified, find method returns all documents.
       Ex:

        > db.sample.find({})
         { "_id" : 1, "no" : 12, "name" : "abc", "item" : "aaa" }
         { "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "abc", "item" : "aaa" }
         { "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "abc", "item" : "aaa" }
         { "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
         { "_id" : ObjectId("5a355a053f46e9bcf9838478"), "no" : 4, "name" : "eee", "pin" : 12345 }
         { "_id" : ObjectId("5a355a053f46e9bcf9838479"), "no" : 5, "name" : "fff" }

        > db.sample.find({no:12})
         { "_id" : 1, "no" : 12, "name" : "abc", "item" : "aaa" }
         { "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "abc", "item" : "aaa" }
         { "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "abc", "item" : "aaa" }

        > db.sample.find({no:{$lt:5}})
         { "_id" : ObjectId("5a355a053f46e9bcf9838477"), "no" : 3, "name" : "ddd", "add" : "abc" }
         { "_id" : ObjectId("5a355a053f46e9bcf9838478"), "no" : 4, "name" : "eee", "pin" : 12345 }

        > db.sample.find({no:12},{no:1, name:1})
         { "_id" : 1, "no" : 12, "name" : "abc" }
         { "_id" : ObjectId("5a3557c23f46e9bcf9838472"), "no" : 12, "name" : "abc" }
         { "_id" : ObjectId("5a3557c33f46e9bcf9838473"), "no" : 12, "name" : "abc" }

2. It returns one document from a collection. And returns first document (as per default order), if the given condition returns multiple documents.

     Syntax:

          db.<<collection_name>>.findOne({<<query>>}, {<<projection>>})

        Ex:
     > db.sample.findOne()
      { "_id" : 1, "no" : 12, "name" : "abc", "item" : "aaa" }

     > db.sample.findOne({no:12})
      { "_id" : 1, "no" : 12, "name" : "abc", "item" : "aaa" }

     > db.sample.findOne({no:12},{no:1, name: 1})
      { "_id" : 1, "no" : 12, "name" : "abc" }

I will post remaining 2 operations in second part.

                                                                                                                                      -- Thiru //                       

No comments:

Post a Comment