In MongoDB, find() with all ($all) operator is used to match and retrieve all the documents where all the specified values contain in the array field. The $all operator matches documents that contain all the elements in a specified array field. It is typically used to find documents where the array contains all the specified multiple fields or array of fields.
Here, we will discuss the $all operator using it with the find() method with examples. So, first, let’s create a collection student and use this to explain the $all operator.
# Create collection
db.student.insertMany([
{
_id: 1,
name: “Steve”,
info: [{“gender”: “male”, “age”: 20}],
hobbies: [“Swimming”, “Reading”],
course: [“MongoDB”, “Python”],
result: [{“marks”:[70,80], “grades”:[“A”,”B”]}]
},
{
_id: 2,
name: “Alexa”,
info: [{“gender”: “female”, “age”: 22}],
hobbies: [“Playing”, “Drawing”],
course: [“Java”, “Python”],
result: [{“marks”:[80,99], “grades”:[“A”,”C”]}]
},
{
_id: 3,
name: “Alice”,
info: [{“gender”: “female”, “age”: 22}],
hobbies: [“Gym”, “Travelling”],
course: [“C++”, “Python”],
result: [{“marks”:[80,99], “grades”:[“B”,”C”]}]
},
{
_id: 4,
name: “Stefan”,
info: [{“gender”: “male”, “age”: 23}],
hobbies: [“Swimming”, “Reading”],
course: [“MongoDB”, “Perl”],
result: [{“marks”:[85,95], “grades”:[“A”,”B”]}]
}
]
)
1. MongoDB find() with $all Operator Syntax
Following is the syntax of the MongoDB find() with the $all operator.
# Syntax
db.collection.find(
{ <field>: { $all: [ <value1> , <value2> … ] } }
)
Here, the <field> should be of array type.
2. MongoDB find() with $all Usage with Example
Use the find() with the $all operator in MongoDB to find the documents where all the specified values contain within the specified array field. In other words, the $all operator retrieves the documents where the array field contains all the specified fields. Let’s take the following query.
# Usage of $all operator
db.student.find({ hobbies: { $all: [“Swimming”, “Reading”] } })
In the above query, we have called the find() method which finds the document according to the criteria set there. So, we have first specified the array field hobbies that we want to query. Then, the $all operator is used to match the documents where the field contains all of the specified values in the array.
In the end, this query will return all documents from the student collection where the hobbies field is an array that contains an array of elements that can be seen in the output.
More details about the $all operator can be found here.
Alternatively, you can also write the same query by using the $and operator.
# Using $and to achieve the same result
db.student.find(
{ $and: [ { hobbies: “Swimming” }, { hobbies: “Reading” } ] }
)
This example yields the same output as above.
3. Find all documents from another array element in Array
In the above example, we have simply found the documents that match all values in an array. Now here, we will find the document that matches all the specified values from the nested array. Consider the following query to do this.
# Finding document that matches specified values
db.student.find({ “result.marks”: { $all: [80, 99] } })
In the query above, we have again called the find() method of MongoDB to find the document that matches all the given elements of the nested array. However, we have given an array field name where result is the embedded document and marks is the name of the array field within the embedded document.
The $all query operator is called that matches documents where the field contains all of the specified elements in the array. The output is retrieved here which shows the related documents.
For more examples refer to Find Document from an Array that Contains a Specific value in MongoDB.
4. Using $all with nested array to find documents
To match documents where the field contains the nested array as an element, for example, field: [ [ “A” ], … ] , pass the nested array as a value to the $all expression of MongoDB find(). Here is an example.
# Finding document that matches specified values
db.student.find({ “hobbies”: { $all: [[“Gym”,”Travelling”]] } })
The student collection I am using doesn’t have the nested array, I will leave this to you to create a collection and run the above example.
Alternatively, you can also write the above query using the $and operator.
# Using $and
db.student.find( { $and: [ { hobbies: [ “Gym”, “Travelling” ] } ] } )
5. Conclusion
In conclusion, we have explored the find() with $all operator of MongoDB with the examples. The $all is helpful when dealing with arrays to match documents that contain all the elements in a specified array field. And this can be coupled with other query operators to create more complex queries.
The $all operator matches documents that contain all the elements in a specified array field. It is typically used to find documents where the array contains all the specified multiple fields or array of fields.
In MongoDB, find() with all ($all) operator is used to match and retrieve all the documents where all the specified values contain in the array field. The $all operator matches documents that contain all the elements in a specified array field. It is typically used to find documents where the array contains all the specified Read More MongoDB