Before reading this article, I highly recommend reading the following previous parts of the series:
In the previous article we learned about the insert method. Today we will learn about the find method of MongoDB databases. In a relational database we use a “Select * From Table_Name” query to fetch the data from a table. However we use the “find” method in MongoDB to retrieve data from a collection. The find method contains many properties and options, we can't understand the find method in a single article. So I divided this method into two parts. Today we learn some basic operations of the find method.
Find: The find method selects documents from a collection and returns a cursor to the selected documents.Syntax: db.Collection_Name.find( query , projection ).Parameters:
The find method does it's work in the following 3 steps:
Actually the find method returns a cursor to selected documents. This cursor automatically displays 20 records on the screen. We can use it command to continue the iteration.First we create a collection and insert some data into the collection then we retrieve these records using the find method.Create a collectionNow insert some data into the preceding collection as in the following:In the Employee_Details collection we insert 8 records (documents). Now we retrieve these records using the find method in various forms.Find() MethodThe find method will display all the records of the collection in an unstructured format.Syntax: db.Collection_Name.find()ExampleWe can see that the find method returns data in a non-structured manner. That is very hard to read. So we can use the pretty() method to show the data in a structured manner.Pretty() MethodThe Pretty method shows data in a structured (formatted) manner.Syntax: db.Collection_Name.find().pretty()ExampleNow the preceding output is very easy to read and understand.Find method with Conditional operatorsSimilar to a relational database MongoDB supports conditional select operations. MongoDB supports a number of conditional operators. Some commonly used conditional operators are the following.
Now we execute some query for each conditional operator.Query 1: Retrieve the record of all employees where City is equal to Delhi.OutputQuery 2: Retrieve the record of all Employees where the age of the employee is less than 23.OutputQuery 3: Retrieve the record of all Employees where the Salary of the employee is greater than 40000.OutputQuery 4: Retrieve the record of all Employees where the age of the employee is greater than or equal to 24.OutputQuery 5: Retrieve the record of all Employees where the Salary of the employee is less than or equal to 39000.OutputQuery 6: Retrieve the record of all employees where the City of the Employee is not equal to Delhi.OutputFind Method with Multiple Conditional operatorsSometimes we must apply multiple conditional operators in a find method. So MongoDB provides various methods for such requirements. Today we will explain some methods.AND ConditionUsing an AND condition we can apply multiple conditional operators. The result will be to retrieve when all the conditions become True. We can pass multiple keys by separating them with a ','. Then MongoDB treats it as an AND condition. Syntax: db.Collection_Name.find( {Key1:Value1 , Key2:value2,……..Keyn:Valuen })Query: Retrieve the record of all employees where the City of the Employee is not equal to Delhi and the salary is greater than 45000.OutputOR ConditionUsing an OR condition we can apply multiple conditional operatora. The result will be retrieved when one or more conditions are True. We need to use the $or keyword for an OR condition.Syntax: db.Collection_Name.find ( { $or:[ { Key1: Value1 }, {Key2:Value2}, ……..{ Key2: Value2} ] } )Query: Retrieve the record of all employees where the age of the Employee is greater than 25 or the salary is greater than 45000.OutputIn ConditionThe In condition is similar to the in condition of relational databases. The In condition is useful when we want to specify more than one value for a single key. We need to use the $in keyword for an In condition. We can retrieve the same result using the OR conditional operator that we retrieve using the $in keyword.Query: Retrieve the record of all employees where the age of the Employee is equal to 23 or 27.First we retrieve the result using an OR conditional operator.Now we retrieve the same result using an IN operator.We can see that the output of both queries are the same.Use AND and OR operator togetherThe following example shows a demo of how to use AND and OR conditional operators together.Query: Retrieve the record of all employees where the Salary of the employee is greater than 50000 and the age of the Employee is equal to 23 or 27.OutputUse AND and IN operator togetherThe following example shows a demo of how to use the AND and IN operators together.Query: Retrieve the record of all employees where the city of the employee is not equal to Delhi and the age of the Employee is equal to 23 or 27.OutputToday we learned the find method and the use of conditional operators with the find method. In the next article we learn some more about the find method.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: