【发布时间】:2015-11-09 23:07:14
【问题描述】:
我正在尝试使用 $and 运算符与 $or 运算符结合使用 php 执行 mongo find,但是我似乎无法弄清楚执行它的方法。此查询试图完成的是搜索folder=>INBOX 并匹配recipient 或from 的电子邮件查询。以下是我的尝试:
查询:
$query['$and'][]['$or'][]['to']['$regex'] = new MongoRegex("/".$_GET['term'].".*/i");
$query['$and'][]['$or'][]['from']['$regex'] = new MongoRegex("/".$_GET['term'].".*/i");
$query['$and'][]['$or'][]['recipient']['$regex'] = new MongoRegex("/".$_GET['term'].".*/i");
$query['$and'][]['folder'] = $_GET['folder'];
打印前:
Array
(
[$and] => Array
(
[0] => Array
(
[$or] => Array
(
[0] => Array
(
[to] => Array
(
[$regex] => MongoRegex Object
(
[regex] => example@example.com.*
[flags] => i
)
)
)
)
)
[1] => Array
(
[$or] => Array
(
[0] => Array
(
[from] => Array
(
[$regex] => MongoRegex Object
(
[regex] => example@example.com.*
[flags] => i
)
)
)
)
)
[2] => Array
(
[$or] => Array
(
[0] => Array
(
[recipient] => Array
(
[$regex] => MongoRegex Object
(
[regex] => example@example.com.*
[flags] => i
)
)
)
)
)
[3] => Array
(
[folder] => INBOX
)
)
)
【问题讨论】:
-
唯一复杂的地方是你把它弄得太复杂了,而这本来应该是一个简单的操作。你真的想在这里做什么?在“任何”提供的字段(to、from、receiver)中搜索输入?您查看过
$or的手册页吗?这里的语法确实偏离了轨道,所以我主要想知道为什么你会认为这是正确的。 -
@BlakesSeven 好吧,我想用这个查询来完成的是在集合中搜索与
folder=>INBOX字段匹配的文档,但还要在recipients或from字段中包含电子邮件跨度>
标签: php mongodb mongodb-query