Showing posts with label mongoDB Basics. Show all posts
Showing posts with label mongoDB Basics. Show all posts

How to Rename Document field in MongoDB and $rename Operator

Rename the Document field in mongoDB

 Sometime We want to Rename the Document field in Collection MongoDB using $rename Operator. Again by using collection method we can do it easily. whenever we want to rename the document field we have to use $rename operator.

db.collectionName.update( criteria, objNew, upsert, multi )

Criteria : This will hold the criteria for updating the document. Here you can specify the condition whether you want to update all the document or few document in collection.

objNew :  $operator which will manipulate the object.

upsert : It is Upsert Operation which will insert the record if it doesn't exist.

multi : Indicates matched documents criteria should be updated or just one Document.


MongoDB Rename Document field


Step 1 :  Create DB and Collection

Create studentInfo DB in mongoDB using below command as well as create student collection and insert Document in it.

create DB and implement rename operator
  

                       

Now we will have some document sample from student collection. By using below query we will fetch document.

db.student.find().pretty()

Output of Above query is below.

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1,
        "exam_status" : "PASSED"
}

Rename the Document field "name" as "fullName"


In the above document we will rename the field "name" using below query which uses $rename operator to do that.

db.student.update({},{$rename :{"name":"fullName"}}, false, true)

This above query will rename the document filed. after executing this we will fetch the document again. Output is below.

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1,
        "fullName" : "M Kumar"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1,
        "exam_status" : "PASSED",
        "fullName" : "MOHAMMED ISHAQ  H"
}


Add a new field to all documents in a collection in MongoDB

How to add new fields in document in mongoDB Collection
Sometime You want to add new fields in existing document in MongoDB collection. You must follow this. Lets Start with example and we will add the new field in the document.

Adding new Field in Existing Document Collection



Create studentInfo Database in MongoDB server. Use the below code for creating the DB. Use db.createCollection("collection-Name")  and create student Collection and insert Document in mongoDB.

  

                       

Insert Some Document in student collection where further we can apply the MongoDB Collection Methods to add new field in existing document in student Collection. Inserted document in student Collection. Below is the document list present in student collection.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

Add New fields in existing Document in Collection

Syntax for Update method in MongoDB is below. By using the below customize code we will add new field in document.

db.collection.update( criteria, objNew, upsert, multi )

Criteria : This will hold the criteria for updating the document. Here you can specify the condition whether you want to update all the document or few document in collection.

objNew :  $operator which will manipulate the object.

upsert : It is Upsert Operation which will insert the record if it doesn't exist.

multi : Indicates matched documents criteria should be updated or just one Document.

Adding  new field in the student Collection

Now we will Add new Field named status in student Collection document and set the value to 1. Below is the code for your reference.

db.student.update({}, {$set :{"status": 1}}, false, true)

Above command will add the new field in existing document of Student Collection. Run the command and again fetch the document using find() method you will see that all the document got updated with status filed.

db.student.find().pretty()

All the document have the new field named with status and value is assigned to 1.

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1
}

Add new Filed exam_status as Pass in Document if marks > 60

Query to add new field exam_status as pass if the marks greater then 60. Below is the code which will add new fields in student collection. 

db.student.update({marks : {$gt :60}}, {$set :{"exam_status" : "PASSED"}}, false, true)

this above query will add the exam_status field  with value pass if  the marks is greater then 60. check the below code and the output.


db.student.find().limit(3).pretty()

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1,
        "exam_status" : "PASSED"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42",
        "isOpen" : false,
        "status" : 1,
        "exam_status" : "PASSED"
}

MongoDB Aggregate Query and Aggregation Pipeline Operator

mongoDB Aggregate Query with Example

MongoDB Aggregation Pipeline Operator

We will perform all the mongoDB Aggregation Operator on studentInfo DB. Create studentInfo Database in MongoDB server. Use the below code for creating the DB.

  

Insert Some Document in student collection where we can apply the MongoDB Collection Methods to explore further usage. Inserting five document in Student Collection. Below is the document list present in student collection.

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

Aggregation Pipeline Stages


MongoDb Aggregation purpose is to process the data to get the computed Result. For executing that we use db.collectionName.aggregate() method.

Aggregation Syntax is     db.collectionName.aggregate( [ { <stage> }, ... ] )

$toLower() Aggregate Function


$toLower function in mongoDB convert the given strings in lower case and return the result. Below is the code for your Reference purpose.
db.student.aggregate([{$project :{name :{$toLower:"$name"}}}])
As above code will convert the document name field in lower case. Below is the output.
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"), "name" : "m kumar" }
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"), "name" : "mohammed ishaq  h" }
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"), "name" : "anbuselvam s" }
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"), "name" : "martinpriyadoss j" }
{ "_id" : ObjectId("5ebf58376803d36568082c60"), "name" : "jagan j" }


$toUpper() Aggregate Function


















Mongodb Cursor Methods and Example

Cursor method in mongoDB

MongoDB Cursor Method with Example


Cursor in MongoDB is defined as a returned Collection Document when find method execution happens. Cursor is like array of a pointer which point specific Index. 

We will perform all the mongoDB Shell Cursor methods on studentInfo DB. Create studentInfo Database in MongoDB server. Use the below code for creating the DB.

  

Using db.createCollection() method, create collection in database. Command to create studnet collection in studentInfo Database is below 


Insert Some Document in student collection where we can apply the MongoDB Collection Methods to explore further usage. I am Inserting five document in Student Collection. Inserted document in student Collection. Below is the document list present in student collection.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

MongoDB Cursor Count() Method



Count() Method will return the document count in specified MongoDB collection. O/P of the above query is 5 as per the above document inserted in it.

db.student.find({marks :{$lt : 70}}).count()

Output of the above query will be 3.

MongoDB Cursor Limit() Method

limit() Method in MongoDB will restrict the document count output while we fetch using find method.. 

db.student.find({$or :[{id :{$lt : 2}},{marks :{$gt:80}}] }).pretty()
O/P :
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}
db.student.find({$or :[{id :{$lt : 2}},{marks :{$gt:80}}] }).limit(2).pretty()

mongoDB cursor Limit method
Output :
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}

MongoDB Cursor Pretty() Method 


Cursor Pretty() method will output the fetched collection document in readable format. Without pretty() method all the Collection document will come row by row. Below is the example with and without pretty() method

db.student.find({marks : {$gt : 60}})

Without MongoDB Pretty Method

db.student.find({marks : {$gt : 70}}).pretty()
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

MongoDB Cursor Sort() Method


Cursor sort() Method in mongoDB hep us to arrange the Specified collection document in sorting order of Ascending or Descending Order. Here Sorting In ascending order is represented by 1 and for descending order it is -1. Below is the Example code , how to use sort() method in MongoDB.

db.student.find({marks :{$lte : 90 }}).sort({marks:1}).pretty()

Cursor Sort() in Ascending Order in MongoDB

Output of the above sort() method code is below as a result. Here in the above code we arranged the document in ascending order based on the mark column.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}

Sort() in MongoDB in Descending Order

db.student.find({marks : {$lte : 90}},{name:1,marks:1,_id:1}).sort({name:-1}).pretty()
Above command will arrange the document collection in descending order based on the name. Here -1 is for descending order, name is the document field.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "name" : "MOHAMMED ISHAQ  H",
        "marks" : 67
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "name" : "MARTINPRIYADOSS J",
        "marks" : 43
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "name" : "M Kumar",
        "marks" : 54
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "name" : "ANBUSELVAM S",
        "marks" : 87
}

Cursor Skip() method in mongoDB


db.student.find({marks:{$lt : 80}},{name:1,marks:1,_id:0}).sort({name:1}).pretty()

This above query will give the output which is below and without skip() method.

{ "name" : "M Kumar", "marks" : 54 }
{ "name" : "MARTINPRIYADOSS J", "marks" : 43 }
{ "name" : "MOHAMMED ISHAQ  H", "marks" : 67 }

Now we will apply limit and get the document.

db.student.find({marks:{$lt : 80}},{name:1,marks:1,_id:0}).sort({name:1}).limit(1).pretty()

Output of the above query will be

{ "name" : "M Kumar", "marks" : 54 }

see the output of both the query, first query have given three document as a output and the second query have given one document because limit is set on the query.

Now we will apply skip() mongoDB Method with limit then we will understand the difference.

db.student.find({marks:{$lt : 80}},{name:1,marks:1,_id:0}).sort({name:1}).limit(1).skip(1)

Output of the above query is below. In the above query limit is set the first record will be skipped and the second record will be taken if available.

{ "name" : "MARTINPRIYADOSS J", "marks" : 43 }

Cursor toArray() Method in MongoDB

toArray() method return all the collection document as an array from the cursor. If we use toArray() method, it will iterate the cursor and load all the data in RAM. In simple word It return an Array of Document.

toArray() Mongo Method Create the json array using all the cursor Document specified which is the easiest way to pass the result to Application.

db.student.find().limit(2).toArray()
.
toArray() method In MongoDB

See the above MongoDB Query where toArray() method have return the document as a array. If you execute the mongo query without toArray() method it will just return the document collection. Below is the output of below query.

db.student.find().limit(2).pretty()

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}



MongoDB Crud Operation and Mongo DB Crud

MongoDB Crud Operation

MongoDB Crud Operation Basics


Create studentInfo Database in MongoDB server. Use the below code for creating the DB.


Above use studentInfo command will create the DB in MongoDB Server. If studentInfo DB will exist then it will switch to that instead of creating it. To view the created database in the list you must have to create at least one collection in that.

MongoDB provide db.createCollection() method to create collection in database. Command to create studnet collection in studentInfo Database is below 


Press Enter collection will be created in studentInfo Database with the name of student. To view all the collection in the studentInfo Database use below command. Below command will list all the collection present in the studentInfo Database. 

Insert or Create Document in MongoDb Collection


To insert document in the collection in MongoDB Database. We use below two command to insert the document.

  • db.collectionName.insertOne()
  • db.collectionName.insertMany()

Using above two method we will insert the document in the student Collection of studentInfo Database. 

This below command will insert one document in student Collection.

db.student.insertOne(
   { 
     "id":1,
     "name":"Manoj Kumar",
     "dob":"2003-04-07",
     "marks":54,
     "created_at":"2017-11-13 23:33:42"
   }
)

To Insert many document at a time we have to use db.collectionName.insertMany() method . Below is the code for inserting many document at a time. We can pass all the document as a array.

db.student.insertMany([
   { 
     "id":2,
     "name":"MOHAMMED  H",
     "dob":"2002-06-14",
     "marks":67,
     "created_at":"2016-11-13 23:33:42"
   },
   { 
     "id":3,
     "name":"ANBUSELVAM S",
     "dob":"2004-06-18",
     "marks":87,
     "created_at":"2019-11-13 23:33:42"
   }
 ])



Fetch Or Retrieve Document from Collection


To retrieve or fetch the document from the MongoDB collection we use db.collectionName.find() method.
  • db.student.find() method will fetch all the document from the specified MongoDB collection. This above method will fetch all the document in single line which will not be in readable format.
  • db.student.find().pretty() method will fetch and arrange the collection document in some proper readable format. pretty() method will give you the output is below for reference. 

MongoDB Operators and Query Example


We can apply various filter on mongoDB collection document based on certain condition. We can use various operator also for document filter. Below is the query list and condition to demonstrate the filtration.

$eq (=)
$eq will match the value with specified value.
$gt (>)
$gt will check that the value is greater than specified value.
$gte (>=)
$gte will check that the value is greater than equal to specified Value.
$in (IN)
$in will check that specified value is there in range or not.
$nin (NOTIN)
$nin will check that specified value not lies in the range.
$lt (<)
$lt will check that the value is less than the specified value
$lte (=<)
$lte will check that the value is less than the specified value
$ne (!=)
$ne will check whether the value not equal to specified value


$and
Logical AND join multiple query clause and compare multiple clause and return value as True or False.
$not
Logical NOT Check the query expression and return the result.
$nor
Logical NOR join multiple query clause and compare multiple clause
$or
Logical OR  join multiple query clause and compare multiple clause and return value as True or False

Some Query Example using above operator is below for reference with output.

  •  db.student.find({marks:{$eq:43}}).pretty()     O/P is below                                                                                             
    mongoDB Comparision Operator Example

  • db.student.find({marks :{$gte:70}}).pretty()  O/P is below                                                                                 
    $gte operator in MongoDB

  • db.student.find({marks :{$in:[54, 87]}}).pretty()  O/P is below                                                                         

Query Using AND Logical Operator in MongoDB


  • db.student.find({$and :[{name : "JAGAN J"},{marks :98}]}).pretty()                                               {
            "_id" : ObjectId("5ebeaafd50845b5c0ea8bf58"),
            "id" : 5,
            "name" : "JAGAN J",
            "dob" : "2003-04-10",
            "marks" : 98,
            "created_at" : "2012-11-13 23:33:42"
        }


Query Using OR Logical Operator in MongoDB

  • db.student.find({$or :[{name:"JAGAN J"},{marks :{$gt:70}}]}).pretty()                                     {
            "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
            "id" : 3,
            "name" : "ANBUSELVAM S",
            "dob" : "2004-06-18",
            "marks" : 87,
            "created_at" : "2019-11-13 23:33:42"
    }
    {
            "_id" : ObjectId("5ebeaafd50845b5c0ea8bf58"),
            "id" : 5,
            "name" : "JAGAN J",
            "dob" : "2003-04-10",
            "marks" : 98,
            "created_at" : "2012-11-13 23:33:42"
    }

Query Using AND Operator as well as OR in MongoDB


  • db.student.find({marks :{$lt : 99}, $or :[{marks :{$in:[43,98]}},{name : "JAGAN J"}]}).pretty()                                                                                                                                  {
            "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
            "id" : 4,
            "name" : "MARTINPRIYADOSS J",
            "dob" : "2004-03-08",
            "marks" : 43,
            "created_at" : "2012-11-13 23:33:42"
    }
    {
            "_id" : ObjectId("5ebeaafd50845b5c0ea8bf58"),
            "id" : 5,
            "name" : "JAGAN J",
            "dob" : "2003-04-10",
            "marks" : 98,
            "created_at" : "2012-11-13 23:33:42"
    }

MongoDB Update Query

Update query in MongoDB is done using below method.
  • db.collection.updateOne() 
  • db.collection.updateMany() 
  • db.collection.replaceOne()

Delete Operation In MongoDB

Delete Query in MongoDB is done using below method.
  • db.collection.deleteOne() 
  • db.collection.deleteMany() 

Our Feature Post

There is a tree between houses of A and B If the tree leans on As House

    There is a tree between houses of A and B. If the tree There is a tree between houses of A and B. If the tree leans on A’s House, the t...

Our Popular Post