MongoDB

How to use $size operator in MongoDB

MongoDB supports several operator sets that help to produce effective and speedy results. In MongoDB, array operator class consists of multiple operators that are used to retrieve documents by referring to arrays; $size is one of them. The $size operator in MongoDB is used to fetch the document that has an array field of a specific size. The $size only deals with arrays and accepts only numeric values as a parameter.

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:

{array-field: {$size: <length-of-array>}}

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:

> db.laptops.find().pretty()

Text Description automatically generated

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:

> db.laptops.find({Make: {$size: 3}}).pretty()

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“:

> db.laptops.find({Make: {$size: 2}}).pretty()

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:

> db.laptops.find({Make: {$size: 5}}).pretty()

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:

> db.laptops.find({Make : {$exists:true}, $where:'this.Make.length>=4'}).pretty()

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.

About the author

Adnan Shabbir