MongoDB '$or' 和 PHP 中的正则表达式

编程入门 行业动态 更新时间:2024-10-20 16:29:32
本文介绍了MongoDB '$or' 和 PHP 中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道有很多关于MongoDB $ or PHP"的主题,但我无法理解;

I know there are tons of subjects about "MongoDB $or PHP", but I'm unable to understand this;

假设我有一个集合 Compagnies:

$Mongo->Compagnies->insert({"Name":"BoldLine", "Service":"Billing", "Description":"Hello World"}); $Mongo->Compagnies->insert({"Name":"Behobe", "Service":"Development", "Description":"Here we are cool"});

我需要使用正则表达式对该集合执行或"查询.所以我做到了:

My need is to execute an 'or' query on this collection, using regex. So I did:

use \MongoRegex as MR; $regex = new MR ("/B/i"); //Where field contains the letter 'B' $Mongo->Compagnies->find( array( '$or' => array( array( "Name" => $regex, "Service" => $regex, "Description" => $regex ) ) ) );

但是结果是0.为什么?结果不应该是2行?现在,如果我这样做:

But there is 0 result. Why? The result shouldn't be the 2 lines? Now if I do :

$Mongo->Compagnies->find( array( '$or' => array( array( "Name" => $regex, "Service" => $regex ) ) ) );

结果只会是第一行.我不明白为什么第二行与查询不匹配,因为名称中还有B".似乎$or"运算符的作用类似于$and".

The result will be only the first line. I don't understand why the second line doesn't match the query, because there is also 'B' in the Name. It seems the '$or' operator acts like '$and'.

我错过了什么吗?即使一个或多个其他字段不包含此字母,我如何选择在 3 个字段之一中包含字母B"的所有实体?

Did I miss something? How can I select all entities containing the letter 'B' in one of the 3 fields, even if one or more other field doesn't not contain this letter?

谢谢:)

推荐答案

每个或条件应该在不同的数组中.试试这个:

Each or condition should be in different array. Try this:

$Mongo->Compagnies->find( array( '$or' => array( array( "Name" => $regex, ), array( "Service" => $regex, ), array( "Description" => $regex, ), ) ) );

更多推荐

MongoDB '$or' 和 PHP 中的正则表达式

本文发布于:2023-11-22 13:26:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   MongoDB   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!