Hello everyone! In my previous blog, I explained CRUD operations in MongoDB you can find this blog Here. In this blog, I will explain some leftover parts like sorting, projection, comparison query operator, Logical query operator and many more

Before starting let’s insert a document first

>db.students.insertMany([
{
"name":"Shubham",
"age":24,
"address":"Noida"
},
{
"name":"Rahul",
"age":20,
"address":"Noida"
},
{
"name":"Karan",
"age":18,
"address":"Noida"
},
{
"name":"Aman",
"age":20,
"address":"Noida"
}
])

Comparison Query Operator:-

$eq:  Matches values that are equal to a specified value.

  • Syntax : { : { $eq: } }

Example: you can use the following query to retrieve all students who have age 24.

>db.students.find( { "age" : { $eq : 24 } });

$gt: – Matches values that are greater than a specified value.

  • Syntax : { : { $gt: } }

$gte – $gte selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value

  • Syntax : { : { $gte: } }

$lt:  $lt selects the documents where the value of the field is less than (i.e. <) the specified value.

  • Syntax : { : { $lt: } }

$ne : – selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.

  • Syntax : { : { $ne: } }

$in : – The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:

  • Syntax : { field: { $in: [, , … ] } }

$nin: – The $nin operator selects the documents where the value of a field does not equals any value in the specified array. To specify an $nin expression, use the following prototype:

  • Syntax : { field: { $nin: [, , … ] } }

Example: –

>db.students.find( { "age" : { $nin :[18,24] } });

Logical Query Operators:-

$and: Returns all documents that match the conditions of both clauses.

  • Syntax: { $and: [ { }, { } , … , { } ] }

Example : –

>db.students.find( { $and : [
... {
... "name" : "Rahul"},{"age" : 20 }
... ]});

Result : –

{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul”, “age” : 20, “address” : “Noida” }

$not: – Returns documents that do not match the query expression.

>db.students.find( { age: { $not: { $gt: 20 } } } )

Result : –

{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul”, “age” : 20, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447b”), “name” : “Karan”, “age” : 18, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447c”), “name” : “Aman”, “age” : 20, “address” : “Noida” }

$nor: – Returns all documents that fail to match both clauses.

>db.students.find( { $nor: [ { "name": "Shubham" }, { "age": 20 } ] } );

Result: –
{ “_id” : ObjectId(“5b002062cca46cacf6c7447b”), “name” : “Karan”, “age” : 18, “address” : “Noida” }

$or: – Returns all documents that match the conditions of either clause.

>db.students.find( { $or: [ { "name": "Shubham" }, { "age": 20 } ] } )

Result :-
{ “_id” : ObjectId(“5b002062cca46cacf6c74479”), “name” : “Shubham”, “age” : 24, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul”, “age” : 20, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447c”), “name” : “Aman”, “age” : 20, “address” : “Noida” }

$exists : –

  • Syntax: { field: { $exists: } }

When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.

Example : –

>db.students.find({"address" : { $exists: true}});

Result : –

{ “_id” : ObjectId(“5b002062cca46cacf6c74479”), “name” : “Shubham”, “age” : 24, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul”, “age” : 20, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447b”), “name” : “Karan”, “age” : 18, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447c”), “name” : “Aman”, “age” : 20, “address” : “Noida” }

Projection:- projection is used for filter your retrieved data. If we want to retrieve only specific filed not all.

let’s take an example if You want to retrieve all data from students who have age 20. But in the result, you don’t want “_id” filed.

>db.students.find({age:20},{"_id" : 0})

Result : –

{ “name” : “Rahul”, “age” : 20, “address” : “Noida” }
{ “name” : “Aman”, “age” : 20, “address” : “Noida” }

if you want to retrieve only student name then for one field you dont need to specify all fields value as Zero. There is another way to retrieve, just specify the value of that field as one.

Example :

>db.students.find({age:20},{"name" : 1})

Result : –

{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447c”), “name” : “Aman” }

cursor.limit(): – Use the limit() method on a cursor to specify the maximum number of documents the cursor will return

  • Syntex : db.collection.find().limit()

Example:-

>db.students.find().limit(2)

Result:-

{ “_id” : ObjectId(“5b002062cca46cacf6c74479”), “name” : “Shubham”, “age” : 24, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul”, “age” : 20, “address” : “Noida” }

Cursor.skip():-  This method is basically used for skip your collected data.

>db.students.find().skip(2)

Result : –

{ “_id” : ObjectId(“5b002062cca46cacf6c7447b”), “name” : “Karan”, “age” : 18, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447c”), “name” : “Aman”, “age” : 20, “address” : “Noida” }

To sort your data : –

Ascending order:- To sort your data in ascending order, use the following command –>

db.students.find().sort({"age":1})

Result:-

{ “_id” : ObjectId(“5b002062cca46cacf6c7447b”), “name” : “Karan”, “age” : 18, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul”, “age” : 20, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447c”), “name” : “Aman”, “age” : 20, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c74479”), “name” : “Shubham”, “age” : 24, “address” : “Noida” }

Descending order:-  To sort your data in descending order, just use the above command with field value -1 instead of 1

db.students.find().sort({"age":-1})

Result:-{ “_id” : ObjectId(“5b002062cca46cacf6c74479”), “name” : “Shubham”, “age” : 24, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447a”), “name” : “Rahul”, “age” : 20, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447c”), “name” : “Aman”, “age” : 20, “address” : “Noida” }
{ “_id” : ObjectId(“5b002062cca46cacf6c7447b”), “name” : “Karan”, “age” : 18, “address” : “Noida” }

I hope this will help you to do some more basic operations on MongoDB. 🙂

knoldus-advt-sticker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.