• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

How to fix MongoDB Error Code – 21 – EmptyArrayOperation

January 2, 2024

How to Fix MongoDB Error Code – 21 – EmptyArrayOperation

MongoDB is a popular NoSQL database that offers high performance, scalability, and flexibility. However, like any other software, it can encounter errors that need to be resolved. One such error is MongoDB Error Code – 21 – EmptyArrayOperation. In this article, we will explore the causes of this error and provide step-by-step solutions to fix it.

Understanding MongoDB Error Code – 21 – EmptyArrayOperation

When you encounter MongoDB Error Code – 21 – EmptyArrayOperation, it means that you are trying to perform an operation on an empty array. This error typically occurs when you use the $push or $addToSet operator to add elements to an array field that does not exist or is empty.

Here’s an example of a MongoDB query that can trigger this error:

db.collection.update(
   { _id: ObjectId("1234567890") },
   { $push: { arrayField: { $each: [1, 2, 3] } } }
)

In this example, if the arrayField does not exist or is empty, MongoDB will throw Error Code – 21.

Fixing MongoDB Error Code – 21 – EmptyArrayOperation

To fix MongoDB Error Code – 21 – EmptyArrayOperation, you can follow these steps:

Step 1: Check if the Array Field Exists

Before performing any operations on an array field, it is crucial to ensure that the field exists. You can use the $exists operator to check if the field is present in the document. Here’s an example:

db.collection.find({ _id: ObjectId("1234567890"), arrayField: { $exists: true } })

If the query returns a document, it means that the array field exists, and you can proceed with the operation. Otherwise, you need to handle the case where the array field is missing or empty.

Step 2: Handle Missing or Empty Array Field

If the array field is missing or empty, you have a few options to handle this situation:

Create the Array Field

If the array field is missing, you can create it using the $set operator. Here’s an example:

db.collection.update(
   { _id: ObjectId("1234567890") },
   { $set: { arrayField: [] } }
)

This query creates an empty array field if it does not exist.

Use $addToSet Instead of $push

If the array field exists but is empty, you can use the $addToSet operator instead of $push. The $addToSet operator adds elements to an array only if they do not already exist. Here’s an example:

db.collection.update(
   { _id: ObjectId("1234567890") },
   { $addToSet: { arrayField: { $each: [1, 2, 3] } } }
)

This query adds elements to the array only if they are not already present.

Step 3: Retry the Operation

Once you have handled the missing or empty array field, you can retry the operation that triggered the error. The error should no longer occur if the array field exists and is not empty.

Summary

In this article, we discussed MongoDB Error Code – 21 – EmptyArrayOperation and provided step-by-step solutions to fix it. Remember to check if the array field exists before performing any operations on it. If the array field is missing or empty, you can create it or use the $addToSet operator instead of $push. By following these steps, you can resolve the EmptyArrayOperation error and continue working with MongoDB seamlessly.

For more information about VPS hosting solutions, visit Server.HK.

Recent Posts

  • How to Migrate from CentOS 8 to AlmaLinux or Rocky Linux Safely
  • CentOS Server Performance Tuning: Optimization Techniques for 2026
  • How to Configure SELinux in CentOS Without Breaking Your System (CentOS Stream 9/10 – 2026)
  • Managing Users and Permissions in CentOS Stream: Best Practices (CentOS Stream 9/10 – 2026)
  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot