簡體   English   中英

mongodb $ nin在php中不起作用

[英]mongodb $nin not work in php

當前代碼:

$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的錯誤嗎?

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

這是行不通的,因為這些鍵具有相同的名稱,而一個鍵會覆蓋另一個鍵。 因此,“鍵”必須是唯一的。

如果您對同一個鍵有兩個條件,則使用$and運算符,該運算符帶有一個參數數組:

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

或者對於JSON而言:

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

這是一個有效的結構,您編寫的不是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM