【发布时间】:2012-05-14 03:29:13
【问题描述】:
-->请查看底部的更新,我已经在 Mongo shell 中复制了同样的问题
我的标准是通过 GET 参数传递的,并被放入 $data(作为 assc 数组)。 然后它进入下面的部分,其中每个搜索词变得部分不完全匹配。之后,我们删除(未设置)空标准。
总共可能有 15 个字段,至少可以设置 1 个,最多可以设置 15 个。
foreach ($data as $k => $v) {
// Make them partial match
$data[$k] = new MongoRegex('/'.$v.'/i');
// Remove empty criteria
if (empty($v)) unset($data[$k]);
}
// Run the search
$cursor = $this->collection->find($data);
只要通过的标准是 4 或更少,一切都很顺利:
Array (
[phone] => MongoRegex Object ( [regex] => 433 [flags] => i )
[property_name] => MongoRegex Object ( [regex] => west [flags] => i )
[city] => MongoRegex Object ( [regex] => H [flags] => i )
[zip_postcode] => MongoRegex Object ( [regex] => 9 [flags] => i )
)
这会返回 5 个结果。
但是在第 4 个之后添加的任何标准都无效。
Array (
[phone] => MongoRegex Object ( [regex] => 433 [flags] => i )
[state_province] => MongoRegex Object ( [regex] => Virginia [flags] => i )
[property_name] => MongoRegex Object ( [regex] => west [flags] => i )
[city] => MongoRegex Object ( [regex] => H [flags] => i )
[zip_postcode] => MongoRegex Object ( [regex] => 9 [flags] => i )
)
这应该返回 2 但仍然返回 5 并且无论添加多少条件项它都会返回 5
-- 更新--
所以我在 Shell 中尝试过它,令我惊讶的是它在那里也不起作用。
db.property_list.find({phone:/433/, property_name:/west/, city:/h/, zip_postcode:/9/}).count()
返回 5
现在,当我添加第 5 个参数时,它应该将结果缩小到 2,但它无效并且仍然显示计数为 5!!!
db.property_list.find({phone:/433/, property_name:/west/, city:/h/, zip_postcode:/9/, state_province:/Virginia/}).count()
返回 5
通过大量实验,我意识到它只是选择了前 4 个标准!!就像在上一个示例中我将位置切换为 'state_province' 和 'city' 一样,它将是“城市”标准,这将是无效的,因为它将是第 5 个标准!!
-- 新更新--
事实证明,如果我在第 5 个条件上使用普通匹配而不是正则表达式,它将起作用。我是否正确假设 Mongo 只能为 4 个正则表达式标准执行 find() ? 所以这将正确返回 1
db.property_list.find({phone:/433/, property_name:/west/, city:/h/, zip_postcode:/9/, state_province:Virginia}).count()
【问题讨论】:
-
如果你只有“state_province”,也会发生同样的情况吗?你能在 Mongo shell 上复制这个吗?
-
嘿 Derick,感谢您的回复,我也在 Mongo shell 中尝试过它,令我惊讶的是仍然是同样的问题......我几乎可以肯定 Mongo 没有将正则表达式标准限制为 4?! !