MongoDB 和 & OR 条件中的复杂语句



继续我的项目,我需要将一些SQL语句翻译成mongoDB

我的SQL语句是:

Delete from 'table' where proc_id = $xxx and (day_id < $day OR day_id > $anotherDay)

现在我的条件数组是:

$condicion = array(
            'proc_id' => $xxx,
            '$or' => array(
                'day_id' => array(
                    '$lt' => $day,
                    '$gt' => $anotherDay
                    )
                )
            );

在mongo集合中为delete而生成的函数返回时无法删除。。。

请帮忙?

每个"day_id"都在自己的$or参数中:

$query = array(
    'proc_id' = > $xxx,
    '$or' => array(
        array( 'day_id' => array ( '$lt' => $day ) ),
        array( 'day_id' => array ( '$gt' => $anotherDay ) ),
    )
)

这就是$or条件作为可能表达式的"列表"的工作方式。

JSON语法更清晰可见:

{
    "proc_id": $xxx,
    "$or": [
        { "day_id": { "$lt": $day } },
        { "day_id": { "$gt": $anotherDay }}
    ]
}

因为"列表"one_answers"对象"定义之间有着非常明确的区别。$or条件是"对象"的"列表",这意味着您列出了完整的条件,就好像它本身就是一个查询一样。由于这不是在$elemMatch

当然,"DELETE"部分是.remove()方法:

$collection->remove($query)

核心文档SQL到MongoDB映射图中有一些常见的示例和资源,如果这些示例不能立即提供帮助,那么链接的文章和演示应该提供帮助。

最新更新