Jonathan Volij
Jonathan Volij

Reputation: 11

Querying a MongoDB with $all on an empty array doesn't return results

When I query my MongoDB server with a filter using $all and an empty array I don't get any results.

I am using pymongo and when my query looks like this:

my_collection.find({"some_field": some_value, "array_field": {"$all": []}})

I don't get any result. If I change it to this:

my_collection.find({"some_field": some_value})

I do get the results I want.

Is this behavior as should be (and I should not send an $all filter if the array is empty)?

My reference is https://docs.mongodb.com/manual/reference/operator/query/all/

Thanks a lot.

Upvotes: 1

Views: 69

Answers (1)

Belly Buster
Belly Buster

Reputation: 8834

$all is used to query a list and will match when all items in your specified list match with items in the database list; you have no items in the all query, so the query makes no sense; I'm surprised it doesn't throw an error.

Upvotes: 1

Related Questions