为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
万千封印 2019-05-31 03:02:36
我正在用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 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

据我所知,目前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.



查看完整回答
反对 回复 2019-06-01
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

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

查看完整回答
反对 回复 2019-06-01
?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

1

2

3

4

$where['$or'] = [

           ['id' => ['lt'=>0]],

           ['id2' => ['lt'=>1]]

         ];

 

 




查看完整回答
反对 回复 2019-06-01
  • 3 回答
  • 0 关注
  • 272 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信