There are several ways to write a MongoDB query where the size of an array field is greater than 1. Let’s discuss the possible methods to accomplish this. Assume that we have a sample collection student which contains an array field documents
like below.
#Create Collection
db.student.insertMany([
{
_id:1,
name: "Marry",
subjects: [ "python" ],
marks: [ 75, 80, 90 ]
},
{
_id:2,
name: "Robert",
subjects: [ "c++", "java", "python" ],
marks: [ 95, 80 ]
},
{
_id:3,
name: "James",
subjects: [ "csharp", "perl" ],
marks: [ 91 ]
},
{
_id:4,
name: "Jennifer",
subjects: [ "php" ],
marks: [ 70, 80, 90]
}
])
1. Using the $size Operator to Query Array Size Greater than 1 in MongoDB
You can use the $size
operator to query the documents from the collection where the array field size is more than 1.
Here’s an example of the $size
operator that determines all documents in the student
collection, Here the marks
field is an array type that contains more than one element.
Note that we have specified a value 2
to the $size
operator to match the array length which is greater than the value 1
.
# Usage of $size operator
db.student.find({ "marks": { "$size":2 } })
Hence, we get the document in the output whose array field marks
is of size 2
.
More details about the $size
operator can be found here.
2. Using the $expr Operator to to Query Array Size in MongoDB
Similarly, you can use the $expr
operator to find all documents in a MongoDB collection where an array field has more than one element. The $expr
operator allows us to use aggregation expressions in the query.
In this case, we are using $size
as an aggregation operator inside $expr.
It is used to determine the size of the array field marks
for each document, which is then used in the comparison with $gt
. The $gt
operator here compares the size of the marks
array field to the value 1
. It returns only documents where the size of the marks
array field is greater than 1
.
# Usage of $expr operator
db.student.find({
$expr: {
$gt: [{ $size: "$marks" }, 1]
}
})
As a result, the document whose array marks
size is greater than 1
is shown in the output.
3. Using the $where operator to find array size greater than 1
Although, we can use the $where
operator to query the documents where the array size is greater than 1. The $where
operator allows us to write a JavaScript expression to be executed for each document in the collection.
Let’s consider the following query where we’re using the $where
operator to execute a JavaScript expression for each document in the collection. The expression is this.marks.length > 1
, which checks whether the length of the array marks
is greater than 1 for each document.
# Usage of $where operator
db.student.find({ $where: "this.marks.length > 1" })
The output is yielded below from the $where
operator.
4. Using the Aggregation Framework with the $match Operator
Using the aggregation framework is another way to identify the array where the size is greater than 1. Here in the query, we have used the aggregate()
method with the $match
stage. The $match
stage filters documents based on the size of the array subjects
. The $exists
operator checks whether the second element of the array exists. Here, we are using subject.1
to refer to the second element of the array, since MongoDB uses zero-based indexing.
# Usage of aggregation framework
db.student.aggregate([
{ $match: { "subjects.1": { $exists: true } } }
])
Thus, the documents with a size greater than 1 are yielded in the output.
5. Using the $redact operator to find array size greater than 1 in MongoDB
Further, you can also use the $redact
operator to find all documents in a collection where an array field has more than one element. In general, the $redact
operator controls the visibility of fields in documents based on specified access levels.
In the following query, the $redact
operator uses the $cond
operator to specify a conditional statement. When the $size
operator gets the size of the array subjects
then the $gt
operator compares the size of the subject
array to the value 1
, returning true if the size is greater than 1
, and false otherwise.
If the $gt
operator returns true, then we keep the document using $$KEEP
. If it returns false, then we prune the document using $$PRUNE.
# Using redact
db.student.aggregate([
{
$redact: {
$cond: {
if: { $gt: [{ $size: "$subjects" }, 1] },
then: "$$KEEP",
else: "$$PRUNE"
}
}
}
])
The output looks like this for the $redact
query.
6. Conclusion:
In conclusion, querying documents where the array size is greater than a value 1
is easy in MongoDB with the operators explored above. By using these operators in combination, we can quickly find the documents we need based on the size of their array fields.