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.