Search for empty arrays in mongodb atlas

I needed some help with the atlas search aggregation query.
I want to use $search syntax while doing the atlas search :
I have one collection; inside which I have one array field of ObjectIds like so :

arrayFieldOfObjectIds : [ObjectId(62ff26c349a3c47656765434), ObjectId(62ff26c349a3c47656765435)]

Now the array fields can also be empty in some cases like so :
arrayFieldOfObjectIds : []

I have defined the correct index mapping for this field.
My goal is to get all the documents that met the below conditions:

1. Either arrayFieldOfObjectIds doesn't exist.
2. If arrayFieldOfObjectIds does exist, it must be empty.
3. If arrayFieldOfObjectIds does exist, it must be equal to some specified value.

The query I have for arrayFieldOfObjectIds is :

{
  "compound": {
    "should": [
      {
        "compound": {
          "mustNot": [
            {
              "exists": {
                "path": "arrayFieldOfObjectIds1"
              }
            }
          ]
        }
      },
      {
        "equals": {
          "value": ObjectId("62ff26c349a3c47656765434"),
          "path": "arrayFieldOfObjectIds1"
        }
      }
    ],
    "minimumShouldMatch": 1
  }
}

This query doesn’t give me those documents where arrayFieldOfObjectIds does exist and is empty.
Note: I need to use $search syntax and I also don’t want to combine $match with $search as it kills the query performance altogether.

Thanks in advance.

Hi there,

Atlas Search currently does not have an operator which checks for empty arrays. A suggested workaround is to add logic to your application which inserts a default value (e.g. boolean value false) to empty arrays, which you can check for using the equals operator, as you are already doing to check for ObjectIds.

I would encourage you to provide some feedback about your needs here so that others can also vote for it, which will help us drive it forward!

Hope this helps.