php操作mongoDB数据库查询的时候怎样写“或”这样的多个条件查询代码?

我正在用php做一个网站,其中有一个在线聊天页面,我需要从mongoDB数据库中查询出发送信息者和接收信息者双方的聊天内容,比如张三和李四聊天,需要查询出张三发送给李四的聊天内容和李四发送给张三的聊天内容,这个查询条件怎么写呢?我把聊天内容插入数据库用的是这样的语句:
$collection = $db->dialogs;
$arr = array("发送者"=>$_SESSION[nick],"发送内容"=>$_GET["sendcontent"],"发送对象"=>$_GET["sendto"],"发送时间"=>$_GET["sendtime"],"发送ip"=>$_GET["sendip"],"会话id"=>$_GET["sessionid"]);
$result = $collection->insert($arr);
我需要查出发送者是张三而发送对象是李四,或者 发送者是李四而发送对象是张三的内容,查询条件怎么写?谢谢!

万千封印
浏览 273回答 3
3回答

哈士奇WWW

据我所知,目前mongoDB没有“或”这个东西但我刚才在网上查了下发现了下面的信息,你参考下吧在mongodb中有$or 操作符的,官网中给出的例子如下:Simple:db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )With another fielddb.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )The $or operator retrieves matches for each or clause individually and eliminates duplicates when returning results. A number of $or optimizations are planned for 1.8. See this thread for details.$or cannot be nested.

收到一只叮咚

我用这样的语句已经查到了想查的结果:$query = array('$or'=>array(array("发送者"=>$_SESSION[nick],"发送对象"=>$_GET["sendto"]),array("发送者"=>$_GET["sendto"],"发送对象"=>$_SESSION[nick]))); $cursor = $collection->find($query);

素胚勾勒不出你

1234$where['$or'] = [           ['id' => ['lt'=>0]],           ['id2' => ['lt'=>1]]         ];  
打开App,查看更多内容
随时随地看视频慕课网APP