【问题标题】:$or-Operator in MongoDB (PHP)MongoDB 中的 $ 或运算符 (PHP)
【发布时间】:2012-10-08 02:17:36
【问题描述】:

大家好,我想通过 $or-Operator 搜索文档... 一开始我要插入 3 个文档:

$aDocument = array(
    'id' => 1,
    'name' => 'WinZIP',
    'shorttext' => 'ZIP / UNZIP Programm' );

$oCollection->insert( $aDocument );

$aDocument = array(
    'id' => 2,
    'name' => 'WinRar',
    'shorttext' => 'ZIP / UNZIP Programm mit RAR-Unterstützung' );

$oCollection->insert( $aDocument );

$aDocument = array(
    'id' => 3,
    'name' => '7zip',
    'shorttext' => 'ZIP / UNZIP Programm mit RAR- und 7z-Unterstützung' );

$oCollection->insert( $aDocument );

然后我正在为我的查询创建一个正则表达式:

$oSearchRegex = new MongoRegex( "/zip/i" );

现在我想在 'name' 或 'shorttext' 中搜索字符串 'zip'... 应该是这样的:

$oCursor = $oCollection->find( array( '$or' => array( array( 'name' => $oSearchRegex ), array( 'shorttext' => $oSearchRegex ) ) ) );

但这不起作用......当我只在一个索引中搜索时,它真的很好......示例:

$oCursor = $oCollection->find( array( 'name' => $oSearchRegex ) );

有什么解决办法吗?

【问题讨论】:

  • 你使用的是哪个版本的mongodb?你的语法看起来很完美
  • 我不知道是不是这个版本,但是当我在服务器上输入“mongo”时,会出现以下输出:MongoDB shell version: 1.4.4.
  • 仅从 1.7.x 版本开始包含“或”运算符
  • 好吧...那我试试更新 mongodb :/

标签: php mongodb mongodb-query nosql


【解决方案1】:

如你所说,你有1.4.4 MongoDB 版本,但"or" operator was included only from version 1.7.x 更高版本

【讨论】:

  • 非常感谢老兄!工作得很好。我现在运行的是 2.2.0 版本,结果很好! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 2019-03-04
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
相关资源
最近更新 更多