簡體   English   中英

我需要在mongo php中使用類似查詢

[英]I need to use a like query in mongo php

我在外殼中使用了此查詢。 工作正常。 如果我在php中使用它,則會顯示一個錯誤

<?php
$m = new Mongo();
$db = $m->foo;
$collection = $db->base;
$query = array( "ro" => '/^a/' );
$cursor = $collection->find($query);

foreach ($cursor as $obj) {
    echo $obj["ro"] . "<br/>";
}

我不確定db.base.find({"ro": /^a/}).limit(10); 查詢工作在PHP?

您需要使用MongoRegex()...

在此處查看文檔: http : //php.net/manual/zh/class.mongoregex.php

因此,PHP代碼將類似於...

$regex = new MongoRegex("/^a/"); 
$where = array('ro' => $regex);
$cursor = $collection->find($where);

如果使用mongodb擴展名,則應使用MongoDB \\ BSON \\ Regex()代替MongoRegex()

$regex = new \MongoDB\BSON\Regex("^a"); 
$where = array('ro' => $regex);
$cursor = $collection->find($where)

您不必使用“ /” [regex]“ /” 只是正則表達式可以正常工作

一行:

$mCollection->find(array("date" => new MongoRegex("/2011-10-25/")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM