2

我有这个例子

db.Wall.runCommand( "text", { search : "See" } );

如何从 PHP 调用它?MongoCollection我在课堂上找不到方法,

注意我正在运行 mongo 2.4 dev 版本。

我尝试使用没有运气的命令方法 Collection 称为 Wall

$ret = $db->command( array(
                        "runCommand" => "Wall",
                        "text" => array('search' => $text))
                    );

输出是

Array ( [ok] => 0 [errmsg] => no such cmd: runCommand [bad cmd] => Array ( [runCommand] => Wall [text] => Array ( [search] => See ) ) )

我找到了答案,但我需要等待 7 小时,因为我的声誉很低 :)

$ret = $db->command( array(
                        "text" => "Wall",
                        "search" => $text)
                    );
4

2 回答 2

4

这是 PHP 中使用 Mongo 文本搜索的更广泛示例

<?php

$m = new MongoClient(); // connect
$db = $collection = $m->foo; // get the database named "foo"
$collection = $db->bar; // get the collection "bar" from database named "foo"

$collection->ensureIndex(
    array(
        'title' => 'text',
        'desc' => 'text',
    ),
    array(
        'name' => 'ExampleTextIndex',
        'weights' => array(
            'title' => 100,
            'desc' => 30,
        ),
        'timeout' => 60000000
    )
);

$result = $db->command(
    array(
        'text' => 'bar', //this is the name of the collection where we are searching
        'search' => 'hotel', //the string to search
        'limit' => 5, //the number of results, by default is 1000
        'project' => Array( //the fields to retrieve from db
            'title' => 1
        )
    )
); 

print_r($result);
于 2013-04-09T11:08:04.287 回答
0
$ret = $db->command( array(
                        "text" => "Wall",
                        "search" => $text)
                    );
于 2013-03-10T08:59:24.950 回答