Haris Hajdarevic
Haris Hajdarevic

Reputation: 1575

Get all documents where column is equal to null

Can someone tell me how to get all documents in Rock Mongo where column 'Namn' exists but have value ''.

This is what I already tried to do:

array (
  'Namn' => 
  array (
    '$in' => '',
  ),
)

But I got this message:

Cannot run command count(): exception: invalid query

I have also tried some other variation of above piece of code. I used $ne insted $in but that also did not gave me expected results.

Thanks

Upvotes: 1

Views: 513

Answers (1)

Avish
Avish

Reputation: 4626

The Mongo query for finding null values is:

{ 'Namn': null }

You seem to be using PHP, so I assume you want something like:

array('Namn' => null)

Note that this will give you documents where the field is null or doesn't exist. If you want only the nulls (but not the documents where the field is missing) you need to use a $type query:

array('Namn' => array('$type' => 6))

See also:

If you're just looking to find an empty-string (''), this is much simpler:

array('Namn' => '')

Should do the job.

In any case you don't need to use $in unless you're looking to match one of a set of different values.

Upvotes: 2

Related Questions