我正在用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);我需要查出发送者是张三而发送对象是李四,或者 发送者是李四而发送对象是张三的内容,查询条件怎么写?谢谢!
3 回答
![?](http://img1.sycdn.imooc.com/5333a1bc00014e8302000200-100-100.jpg)
幕布斯6054654
TA贡献1876条经验 获得超7个赞
据我所知,目前mongoDB没有“或”这个东西
但我刚才在网上查了下
发现了下面的信息,你参考下吧
在mongodb中有$or 操作符的,官网中给出的例子如下:
Simple:
db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )
With another field
db.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.
![?](http://img1.sycdn.imooc.com/54584cb50001e5b302200220-100-100.jpg)
江户川乱折腾
TA贡献1851条经验 获得超5个赞
我用这样的语句已经查到了想查的结果:$query = array('$or'=>array(array("发送者"=>$_SESSION[nick],"发送对象"=>$_GET["sendto"]),array("发送者"=>$_GET["sendto"],"发送对象"=>$_SESSION[nick]))); $cursor = $collection->find($query);
- 3 回答
- 0 关注
- 1325 浏览
添加回答
举报
0/150
提交
取消