• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:7 mins read
You are currently viewing MongoDB Find records where the array field is empty

How to find documents/records from MongoDB collection where the array field is empty? In this article, we will explore how to find records where an array field is empty in MongoDB. Certainly, there are different methods we can use to find records where an array field is empty in MongoDB.

Advertisements

First, let’s insert a few documents with array fields empty in the collection student, with the help of the following query, we have inserted the records in the collection student


# Create Collection
db.student.insertMany([
    { 
        _id:1, 
        course_pass: [ "C++", "Python", "Java" ],
        course_fail: [ ]
      
    },
    { 
        _id:2, 
        course_pass: [ ],
        course_fail: [ "Data Structure", "Calculus" ]
      
    },
    { 
        _id:3,
        course_pass: [  ],
        course_fail: [ "Finance", "Marketing" ]
      
    },
    { 
        _id:4,
        course_pass: [ "Accounts", "Finance", "Marketing" ],
        course_fail: [  ]
      
    }
])

1. Use the $size Operator to Find in MongoDB where the Array field is Empty

The $size operator is a MongoDB query operator that returns records where the specified array field’s size matches a particular value however, we can use the $size operator to match records where the array field is empty. In the following query, we find the records whose array course_pass is empty. For this, the course_pass is provided with the $size operator that has the value 0.


# Usage of $size operator
db.student.find({ course_pass: { $size: 0 } })

The output displayed the records whose array course_pass is null or empty. 

MongoDB Find array empty

More details about the $size operator can be found here.

2. With the $exists operator to find records in MongoDB where the array field is empty

We can also utilize the $exists operator to search the records where the array field is empty. In general, the $exists operator is a MongoDB query operator that returns records where the specified field exists or does not exist.

In the below case, we want to find records where the course_fail array field exists and has an empty array value. The $exists operator ensures that we only get documents where the field exists as the true value is set, and the $eq operator ensures that the field has an empty array value.


# Usage of $exists operator
db.student.find({ course_fail: { $exists: true, $eq:[] } })

There, we yielded two records whose array field course_fail is empty.

MongoDB search array empty

3. With the $not operator to find array with empty

Use the $not operator to perform the task of finding the records/documents where the array field is empty in MongoDB. The $not operator in MongoDB is used to perform a logical NOT operation on a specified expression.

Here, we have used the $not operator in conjunction with the $ne operator to find the records where the array field course_pass is empty. The $ne operator here matches all records where the value of a field is not equal to a specified value and the $not operator then returns records that do not match the specified expression. 


# Usage of $not operator
db.student.find({ course_pass: { $not: { $ne: [] } } })

As can be seen in the output, the record with the empty array field is returned.

MongoDB Find records

4. With the $type operator

Alternatively, there is the $type operator which finds all documents where the field has a BSON data type of 4 (array). As arrays are considered as BSON data types in MongoDB. Here, we have used the $type operator along with the $size operator.

The $size operator gets all the records where the array course_pass is empty and then the $type operator gets only those records where the field array course_pass has a BSON data type of 4 (array).


# Usage of $type operator
db.student.find({ course_pass: { $type: 4, $size: 0 } })

The records are generated that satisfy the above query criteria.

5. With the $where operator

Next, we used the $where operator to match documents where the array field is an empty array. By using the $where operator, we can specify a JavaScript expression that checks the length of the array field and returns true if it is empty. In this case, we want to find documents where the course_fail field is empty. The $where operator checks the length of the course_fail array field. Also, it returns the records of the empty array if it is equal to 0.

#Usage of $where operator 
db.student.find({ $where: "this.course_fail.length === 0" })

In the result, we can see the records whose array course_fail is empty.

MongoDB Find records

6. Conclusion

Concluding here, we have explored different methods to find records/documents where an array field is empty in MongoDB. Each method has its advantages and disadvantages. The choice of method depends on the specific use case and performance considerations.