简体   繁体   English

PHP Mongo查询NOT NULL

[英]PHP Mongo Query NOT NULL

Anyone know the syntax for writing a php-mongo query to use NOT NULL ? 有人知道编写php-mongo查询使用NOT NULL的语法吗?

I know how to do this when I query for NULL : 我在查询NULL时知道如何执行此操作:

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

Is this even possible? 这甚至可能吗?

是的,你想要$ne运算符,所以

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

Basically, the same kind of queries you would use on the Mongo console, you pass as an array to the query methods. 基本上,您将在Mongo控制台上使用相同类型的查询 ,将数组作为数组传递给查询方法。

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: 或者检查它是否不等于null:

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

Watch out for the $ in double quotes, since PHP will consider that a variable. 注意$ in double引号,因为PHP会考虑变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM