Updated March 8, 2023
Definition of MongoDB find in array
MongoDB provides different kinds of functionality to the user; the find array is one of the functionalities that is provided by MongoDB. In the find array functionally we can find the required document as per our requirement. For the implementation of find array we use db.collectName. find () method. The find-in array works on the exact matching. In another word, we can say that a query in which we need to pass all parameters inside the curly base and all parameters depends on the user requirement. The array uses the find method and it is used to return all matching documents from the collection.
Syntax:
db. Specified collectioName. find(specified field name 1: specified value 1, specified value 2,…… specified value N, specified field name 2: specified value 1, specified value 2,…… specified value N )
Explanation
In the above syntax, we use the find () method with different parameters as follows.
- specified collectName: The specified collectName means the actual collection name that we already created.
Find (): It is a method to implement an array. - specified field name 1: It is used to identify the field name from the document with specified values as shown.
- specified field name 2: similarly this is used for the second field from the collection with specified values as shown.
How to find array in MongoDB?
- Now let’s see how to find the array works in MongoDB as follows. There are different ways to implement the find array as follows. First, let’s see how we can use a conditional operator in the array:
- MongoDB provides the different types of conditional operators to the user, so as per our requirement we can use the conditional operator with find () method to implement the array. We have different types of conditional operators such as greater than, less than, and less than equal to, etc. In the second way we can implement nested array as follows:
- MongoDB also provides the functionality to implement the nested array structure as per our requirements. In the event that you don’t have a clue about the record position of the archive settled in the cluster, connect the name of the exhibit field, with a dab (.) and the name of the field in the settled report.
- In the third way, we can use array index with find () method. Utilizing dab documentation, you can indicate inquiry conditions for the field in a report at a specific record or position of the exhibit. The exhibit utilizes zero-based ordering.
- In the next method we can specify more than one condition in array as follows:
When determining conditions on more than one field settled in a variety of records, you can indicate the inquiry to such an extent that either a solitary report meets these conditions or any mix of archives (counting a solitary report) in the exhibit meets the conditions. - In the next method, we can implement a nested array with more than one condition. Use $elemMatch administrator to determine numerous standards on a variety of implanted reports with the end goal that something like one installed archive fulfills every one of the predefined measures. A blend of Elements Satisfies the Criteria. On the off chance that the compound inquiry conditions on an exhibit field don’t utilize the $elemMatch administrator, the question chooses those archives whose cluster contains any blend of components that fulfills the conditions.
Examples
Now let’s see the different examples of find in the array for better understanding as follows. First, we need to create the new collection as follows. First, we need to use a database then create a collection as follows. First, we created a sample database by using the following statement as follows.
use sample
Explanation
In the above statement, we use the command to create the new database, here we successfully created a sample database and we use it. The final result of the above statement we can illustrate by using the following screenshot as follows.
After successful creation of the database, we need to create the collection by using the following statement as follows.
db.createCollection("student")
Explanation
In the above statement, we use createCollection command to create the new collection; here we created a new collection name as a student as shown. The final result of the above statement we can illustrate by using the following screenshot as follows.
Now we have a collection, so now we need to insert the same document into the newly created collection that is a student by using the following statement as follows.
db.student.insert({ name: ["Johan", "Sharma"], dept: "IT", marks: [75, 85], dbms: [56, 65]})
db.student.insert({ name: ["Rohit", "Gupta"], dept: "COMP", marks: [90, 75], dbms: [32, 85]})
db.student.insert({ name: ["Pooja", "Darel"], dept: "COMP", marks: [55, 65], dbms: [45, 35]})
db.student.insert({ name: ["Sameer", "Kohli"], dept: "MECH", marks: [36, 45], dbms: [66, 85]})
db.student.insert({ name: ["Jenny", "Wadia"], dept: "COMP", marks: [47, 53], dbms: [45, 85]})
Now we can perform the find in array method as follows.
db.student.find( { name: ["Rohit", "Gupta"] } )
Explanation
This very simple example of find in the array, here we need to find the first and last name of a student by using the find () method. In this example, we inserted documents in an array format as shown. Here we need to find Rohit and Gupta in the student collection. The final result of the above statement we can illustrate by using the following screenshot as follows.
Now let’s see how we can use the $all operator with the find () method as follows.
db.student.find( { name: { $all: ["Sameer", "Kohli"] } } )
Explanation
The final result of the above statement we can illustrate by using the following screenshot as follows.
Now let’s see how we can use the comparison operator as follows.
db.student.find( { marks: { $gt: 50 } } )
Explanation
In the above example, we try to implement the greater than comparison operator with find array as follows. The final result of the above statement we can illustrate by using the following screenshot as follows.
In the same way we can use greater than and less than comparison operators as follows.
db.student.find( { dbms: { $gt: 30, $lt: 60 } }
Explanation
The final result of the above statement we can illustrate by using the following screenshot as follows.
In the same way, we can implement the size of the array with find () and many more operations as per our requirement.
Conclusion
We hope from this article you learn more about MongoDB find in an array. From the above article, we have learned the basic syntax of the find in an array and we also see different examples of the find in array. From this article, we learned how and when we use MongoDB find in an array.
Recommended Articles
This is a guide to MongoDB find in array. Here we discuss the Definition, How to find in array in MongoDB? examples with code implementation respectively. You may also have a look at the following articles to learn more –