Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Mongo Query NOT NULL

Tags:

php

mongodb

Anyone know the syntax for writing a php-mongo query to use NOT NULL?

I know how to do this when I query for NULL:

<?php
$cursor = $collection->find(array("someField" => null));

Is this even possible?

like image 730
Michael Avatar asked Mar 20 '12 18:03

Michael


2 Answers

Yeah, you want the $ne operator, so

$cursor = $collection->find(array("someField" => array('$ne' => null)));
like image 94
jjm Avatar answered Oct 10 '22 21:10

jjm


Basically, the same kind of queries you would use on the Mongo console, you pass as an array to the query methods.

In your case, it could be (if you're checking that the field exists - note that the field could just be absent from the document):

array("someField" => array('$exists' => true))

Or to check if it's not equal to null:

array("someField" => array('$ne' => null))

Watch out for the $ in double quotes, since PHP will consider that a variable.

like image 39
Tim Lytle Avatar answered Oct 10 '22 19:10

Tim Lytle