2012-11-27 83 views
1

这是我syntaq查询

http://pastebin.com/q05EBSUJ

我对结果有错误

[Tue Nov 27 13:05:16 2012] [error] [client 127.0.0.1] PHP Fatal error: Uncaught exception 'MongoCursorException' with message 'invalid operator: $or' in /var/www/ 

我尝试使用Google,很多的说,如果它在MongoDB的版本错误。

所以我的syntaq错了?或者我的mongodb版本错误,我的mongodb版本= 2.2.1?

感谢

回答

0

您有$或阵列深度的额外层。

尝试:

$result = 
    $mo->find(array(
      "node" => "stream", 
      "$or" => array("user_id" => $this->session->userdata('user_id'), 
           "to" => $this->session->userdata('user_id')) 
     ->limit(5) 
     ->sort(array("time" => -1)); 
0

@tpae其无法正常工作。

我有我自己的解决方案。

$result = $mo->find(
     array('$or' => array(
     array(
      'node' => 'stream', 
      'user_id' => $this->session->userdata('user_id') 
     ), 
     array(
      'node' => 'stream', 
      'to' => $this->session->userdata('user_id') 
     ) 
    ) 
    ) 
)->sort(array('time' => -1))->limit(5); 

及其工作。

+1

您可以移动$的“节点”外或因为它是这两个条款,一个小点,但还是一个值得noteing – Sammaye

0

使用下面的代码

$result = $mo->find(
     array(
     'node' => 'stream', 
     array('or' => array(
      'user_id' => $this->session->userdata('user_id'), 
      'to' => $this->session->userdata('user_id') 
     ) 
    ) 
    ) 
)->sort(array('time' => -1))->limit(5); 

,而不是

$result = $mo->find(
     array(
     'node' => 'stream', 
     array('$or' => array(
      'user_id' => $this->session->userdata('user_id'), 
      'to' => $this->session->userdata('user_id') 
     ) 
    ) 
    ) 
)->sort(array('time' => -1))->limit(5); 
0

请看下面的例子,它使用$or操作员从嵌入文档中选择字段:

$collection->find([ 
    // $or (array) 
    '$or' => [ 
     ['key 1' => 'value 1'], // (array) 
     ['key 2' => 'value 2'] // (array) 
    ] 
]); 

尝试:

$result = $mo->find([ 
    // $or (array) 
    '$or' => [ 
     ['user_id' => $this->session->userdata('user_id')], // (array) 
     ['to'  => $this->session->userdata('user_id')] // (array) 
    ], 
    'node' => 'stream', 
])->sort(['time' => -1])->limit(5); 

References

+0

我只知道的一样,在PHP MongoDB的查询可以使用元组,就像你的。 ['user_id'=> $ this-> session-> userdata('user_id')]。让我尝试 –