Thursday, September 12, 2019

MongoDB Select Query


MongoDB Select Query

Here we are going to learn MongoDB select query, Logical Conditions, Sorting, and Limit.

Select Query with Documents
To display only first data of any collection, we used:
db.student.findOne()



In MongoDB, when we want to write select query with condition, use all conditions in JSON format, means key & value format.

For Example:
Suppose, I want to display student data whose city is Delhi, query will be:
db.student.find({“city”:”Delhi”})

Suppose, I want to display student data whose email is suresh@gmail.com, query will be:
db.student.find({“email”:”suresh@gmail.com”})

Suppose, I want to display student data whose age will be greater than 18, query will be:
db.student.find({“age”:{$gt:”15″}})

In above query, $gt means Greater Than. We use greater than , less than conditions in some different way in MongoDB. To use greater than or less than in following way:



$gt:  Greater Than
$gte:  Greater Than Or Equal To
$lt:  Less Than
$lte:  Less Than Or Equal To
$eq:  Equal To
$ne: Not Equal To


How To Use AND / OR Condition

AND Condition
Suppose, I want to display student data whose city is Delhi and email is suresh@gmail.com, query will be:
db.student.find({“city”:”Delhi”, “email”:”suresh@gmail.com”})

Suppose, I want to display student data whose city is Delhi and age will be greater than 18, query will be:
db.student.find({“city”:”Delhi”, “age”:{$gt:”15″}})

OR Condition
Suppose, I want to display student data whose city is Delhi or state is UP, query will be:
db.student.find({$or:[{“city”:”Delhi”}, {“state”:”UP”}]})

Mixed AND & OR Condition
Suppose, I want to display student data whose name is suresh kumar and city is Delhi or state is UP, query will be:
db.student.find({“name”:”suresh kumar”, $or:[{“city”:”Delhi”}, {“state”:”UP”}]})

Suppose, I want to display document, whose email end with gmail.com, then query will be:
db.student.find({“email”:{$regex:/gmail.com$/}})

Above query will display all the records, whose email ends with gmail.com.

If we want to display documents in Ascending / Descending order, we alsways use a command named sort in MongoDB.

For Example:
Suppose, we want to display documents order by name in Ascending, then query will be:

db.student.find({“state”:”Harayana”}).sort({“name” : 1})

Query result will display documents whose name in Ascending order means a – z format. Suppose, we want to display documents order by name in Descending, then query wil be:

db.student.find({“state”:”Harayana”}).sort({“name” : -1})
Query result will display documents whose name in Ascending order means z – a format.

In mongoDB, If any query gives more than one documents in result and we need only first document, then we use command limit.

For Example:
Suppose, we want to display documents order by name in Ascending and we need only first document, then query will be:

db.student.find({“state”:”Harayana”}).sort({“name” : 1}).limit(1)

Above query will give a single result. If we want to get two document, then we should increase value under limit, means

db.student.find({“state”:”Harayana”}).sort({“name” : 1}).limit(2)

No comments:

Post a Comment