In this informative guide, we will demonstrate the usage of the $size operator in MongoDB:
How $size operator works in MongoDB
We have broken down the primary function of the $size operator in MongoDB into the following steps: Firstly, it matches an array field with respect to the size inserted by the user; and then Fetches the documents that contain the fields that satisfy the above step
The syntax of $size operator is defined as:
Here, array-field refers to the name of the targeted field in a document and length-of-any-array denotes any numeric number that matches the length.
How to use the $size operator in MongoDB
In this guide, we will use the following database and collection names:
- linuxhint is the database that we are going to use here
- laptops will be used as a collection name that links with the linuxhint database
Before digging into examples, let’s get the list of documents present in laptops collection by following command:
Example 1: Basic use of $size operator in MongoDB
This example guides you to get the basic use of $size operator:
Referring to the documents present in “laptops” collection, the command mentioned below will retrieve the document in which the array field is of length 3:
Only one document is retrieved that contains an array length of 3 in the “Make” field.
Example 2: Using $size operator with nested arrays
As the basic use of $size is to get the output that only matches the specified array length. It counts a nested array as a single entity. Let’s say, there is an array that contains a single nested array and one value, the $size operator will not go for the values of the nested array, but it counts it a single value. Thus, the overall length of the parent array would be “2“:
The Mongo query written below will retrieve the documents that have array lengths of “2“:
Although, the nest array contains 2 values in it, but it is considered as one value and therefore the overall length of parent array is 2:
Example 3: Using $size operator with the wrong length
What if you have entered a length that does not match in the targeted collection? Let’s check it using the following command:
The command will be executed but will not show anything because our collection does not have any array of length “5“.
Note: However, you can get the result by using the “$where” operator with “$exists” operator, but the execution would be slow in this case. The command mentioned below will display the documents that have array length greater than or equals to 4:
Conclusion
Array query operators are used in MongoDB to retrieve documents by referring to arrays. The operators that deal with arrays in MongoDB are $size, $all, and $elemMatch. This guide targeted the $size operator and you can get a brief introduction followed by some examples on $size operator in MongoDB. Its primary use is to get the documents from a specific collection by using the length of an array. Although the same functionality can be obtained using $where and $exists operators as well, they take time and a long syntax to do so.