【问题标题】:mongodb $nin not work in phpmongodb $nin 在 php 中不起作用
【发布时间】:2014-08-15 06:54:24
【问题描述】:

当前代码:

$doc = array('ooxx' => array(1,2,3,4,5));

datamodel()->insert($doc);

$doc2 = array('ooxx' => array(6,7,8,9));
datamodel()->insert($doc2);

$macher = array('ooxx'=>array('$exists' => true), 'ooxx' => array('$nin'=>array(6)));
$res = datamodel()->findOne($macher);
print_r($res);

当我用波纹管替换$macher 时,它运行良好,为什么?这是mongodb的bug吗?

$macher = array( 'ooxx' => array('$nin'=>array(6)), 'ooxx'=>array('$exists' => true));

【问题讨论】:

  • $nin 在 php 中确实有效。您是否看到查询返回的任何错误?记录查询结果并查看。

标签: php mongodb mongodb-query


【解决方案1】:

它不起作用,因为键具有相同的名称并且一个覆盖另一个。所以“键”必须是唯一的。

如果您对同一个键有两个条件,则使用$and 运算符,该运算符接受一组参数:

$matcher = array(
    '$and' => array( 
        array( 'ooxx' => array( '$nin' => array(6) ) ),
        array( 'ooxx' => array( '$exists' => true ) )
    )
)

或者对于 JSON 思维:

{
    "$and": [
        { "ooxx": { "$nin": [6] } },
        { "ooxx": { "$exists": true } }
    ]
}

这是一个有效的结构,而你所写的却不是。

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多