Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongo+;PHP查询如何检查字段是否为空_Php_Arrays_Mongodb_Null_Size - Fatal编程技术网

Mongo+;PHP查询如何检查字段是否为空

Mongo+;PHP查询如何检查字段是否为空,php,arrays,mongodb,null,size,Php,Arrays,Mongodb,Null,Size,数据: 我想写一个查询来检查“sub_字段”是否为空 这就是我所尝试的: "field1" : { "sub_field" : [ ]} 显然,当数组不为null时,它会给出结果(我徒劳地尝试了null和空格) 我被告知可以使用“$size”操作符来实现这一点。但我到目前为止运气不好 有什么建议吗?对于查找类型为null或未定义的字段,您可以使用: 对于未定义的: $cursor = $collection->find( array('field1.sub_field' => ar

数据:

我想写一个查询来检查“sub_字段”是否为空

这就是我所尝试的:

"field1" : { "sub_field" : [ ]}
显然,当数组不为null时,它会给出结果(我徒劳地尝试了null和空格)

我被告知可以使用“$size”操作符来实现这一点。但我到目前为止运气不好


有什么建议吗?

对于查找类型为null或未定义的字段,您可以使用:

对于未定义的:

$cursor = $collection->find( array('field1.sub_field' => array('$ne' => null))
对于空值:

db.getCollection('your_collection_name').find({ yuorField: { $type: 6 } })

对于查找类型为null或未定义的字段,您可以使用:

对于未定义的:

$cursor = $collection->find( array('field1.sub_field' => array('$ne' => null))
对于空值:

db.getCollection('your_collection_name').find({ yuorField: { $type: 6 } })

你可以用两种方法来解决这个问题。第一种方法是使用点符号和运算符在查询对象键中使用数字数组索引来搜索至少没有
子字段的所有文档
数组元素:

db.getCollection('your_collection_name').find({ yuorField: { $type: 10 } })
这应该翻译成PHP作为

var cursor = db.collection.find({ "field1.sub_field.0": { "$exists": false } })
另一种方法是使用运算符和运算符(全部包装在运算符中)查找没有
子字段的所有文档,这些子字段可能不存在,也可能是空数组:

$cursor = $collection->find( array("field1.sub_field.0" => array("$exists" => false))

另一种方法,你可以考虑虽然性能较慢,但还是要使用<强> <强>运算符:

var cursor = db.collection.find({
    "$or": [
        { "field1.sub_field": { "$exists": false } },
        { "field1.sub_field": { "$size": 0 } }
    ]
});
对于基准测试,请尝试填充测试集合:

var cursor = db.collection.find({       
    "$where": "this.field1.sub_field.length == 0"   
});
所有这三个查询都将生成具有空
子_字段的文档
数组:

db.test.insert([       
    { field1: { sub_field: [] } },
    { field1: { sub_field: [ "foo" ] } },
    { field1: { sub_field: [ "foo", "bar" ] } }
]);

> db.test.find({ "field1.sub_field.0": { "$exists": false } })
> db.test.find({
    "$or": [
        { "field1.sub_field": { "$exists": false } },
        { "field1.sub_field": { "$size": 0 } }
    ]
})
> db.test.find({ "$where": "this.field1.sub_field.length == 0" })

你可以用两种方法来解决这个问题。第一种方法是使用点符号和运算符在查询对象键中使用数字数组索引来搜索至少没有
子字段的所有文档
数组元素:

db.getCollection('your_collection_name').find({ yuorField: { $type: 10 } })
这应该翻译成PHP作为

var cursor = db.collection.find({ "field1.sub_field.0": { "$exists": false } })
另一种方法是使用运算符和运算符(全部包装在运算符中)查找没有
子字段的所有文档,这些子字段可能不存在,也可能是空数组:

$cursor = $collection->find( array("field1.sub_field.0" => array("$exists" => false))

另一种方法,你可以考虑虽然性能较慢,但还是要使用<强> <强>运算符:

var cursor = db.collection.find({
    "$or": [
        { "field1.sub_field": { "$exists": false } },
        { "field1.sub_field": { "$size": 0 } }
    ]
});
对于基准测试,请尝试填充测试集合:

var cursor = db.collection.find({       
    "$where": "this.field1.sub_field.length == 0"   
});
所有这三个查询都将生成具有空
子_字段的文档
数组:

db.test.insert([       
    { field1: { sub_field: [] } },
    { field1: { sub_field: [ "foo" ] } },
    { field1: { sub_field: [ "foo", "bar" ] } }
]);

> db.test.find({ "field1.sub_field.0": { "$exists": false } })
> db.test.find({
    "$or": [
        { "field1.sub_field": { "$exists": false } },
        { "field1.sub_field": { "$size": 0 } }
    ]
})
> db.test.find({ "$where": "this.field1.sub_field.length == 0" })

我还没有试过解决办法。但有一点值得注意。sub_字段永远不会为空。它可能是一个空数组(0)。但决不为空。这就是我的问题所在。我将尝试解决方案并将结果发布在这里。尽管如此,查询仍然有效。我已经删除了过时的测试用例,可以确认结果仍然是一样的。谢谢。尽管我还没有解决这个问题,但我非常确信这是一条路要走。当我尝试使用'length'选项的'$where'条件时,我收到一个错误:“TypeError:无法读取'field.sub_field.length'附近未定义的属性'length'。我正在使用Mongodb 2.6.11。是否存在任何兼容性问题?据我所知,2.6.11没有任何问题。该错误很可能是由于某些文档没有
sub_字段
键,因此出现了
undefined
错误。这起到了作用:$cursor=$collection->find(数组(“field.sub_field.0”=>array('$exists'=>true));我还没有试过解决办法。但有一点值得注意。sub_字段永远不会为空。它可能是一个空数组(0)。但决不为空。这就是我的问题所在。我将尝试解决方案并将结果发布在这里。尽管如此,查询仍然有效。我已经删除了过时的测试用例,可以确认结果仍然是一样的。谢谢。尽管我还没有解决这个问题,但我非常确信这是一条路要走。当我尝试使用'length'选项的'$where'条件时,我收到一个错误:“TypeError:无法读取'field.sub_field.length'附近未定义的属性'length'。我正在使用Mongodb 2.6.11。是否存在任何兼容性问题?据我所知,2.6.11没有任何问题。该错误很可能是由于某些文档没有
sub_字段
键,因此出现了
undefined
错误。这起到了作用:$cursor=$collection->find(数组(“field.sub_field.0”=>array('$exists'=>true));它不是空值的情况。这是一个空数组的情况。如何在查询中检查数组是否为空?该解决方案非常适合空值。然而,我猜空数组并不等同于“undefined”。对于$Type 6检查,返回值始终为空。它只检查未定义和空字段。这不是空值的情况。这是一个空数组的情况。如何在查询中检查数组是否为空?该解决方案非常适合空值。然而,我猜空数组并不等同于“undefined”。对于$Type 6检查,返回值始终为空。它只检查未定义和空字段。