1

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.

1 Answer 1

1

$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.

Sign up to request clarification or add additional context in comments.

1 Comment

That is quite bad client experience as I would expect that an empty array would be a subset of every array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.