PHP Mongo Query NOT NULL

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

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?

回答1:

Yeah, you want the $ne operator, so

$cursor = $collection->find(array("someField" => array('$ne' => null))); 


回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!