zfcampus / zf-doctrine-querybuilder

Doctrine Query Builder request Filters
BSD 3-Clause "New" or "Revised" License
32 stars 19 forks source link

Semantically "isNull" does not map to "field:{$exists:false)", "field:null" is better #39

Closed Bombdog closed 7 years ago

Bombdog commented 7 years ago

IsNull.php:22 $queryBuilder->$queryType($queryBuilder->expr()->field($option['field'])->exists(false));

If I have a field in my document, "foo" that is set to null, I would expect that filtering as follows:

[
   'field' => 'foo',
   'filter' => 'isNull'
]

Would return my document, but it doesn't. This is the query that runs:

db.getCollection('myCollection').find({foo:{$exists:false}})

I suggest that a much better match would be:

db.getCollection('myCollection').find({foo:null})

If a field is marked as null then it DOES exist and doesn't return. By searching for a null we cover both cases: the case where the field doesn't exist AND the case where the field really IS null.

I already fixed it for isNull, isNotNull ODM filters, I'm just raising this as an issue here in case I missed something? I can put in a pull request if there are no objections.

michalbundyra commented 7 years ago

@Bombdog Please submit your PR with tests if you can. Thanks!

Bombdog commented 7 years ago

@webimpress that's done. I added a nullable description field to the test document to illustrate the change in behaviour.

michalbundyra commented 7 years ago

Resolved in #40