2014-12-19 108 views
1

我有Mongobd文檔。所有文檔都包含全名字符串(例如「john fidgerald kennedy」,「john doe」)。Mongodb。如何查找包含所有子字符串的字符串?

在一個包含子字符串(例如「john」,「kennedy」或「kennedy」,「john」)的網絡查詢中,我想查找全名字符串包含所有這些子字符串的所有文檔(例如「john fidgerald kennedy」 )。

但是,我想不出如何做到這一點。

基本上,PHP,我想這樣做$收藏 - >找到(陣列(「全名」 =>「約翰」 & &「肯尼迪」)。我知道這句法是不可能的,但它是給我想什麼的想法。

任何想法如何做到這一點?理想情況下,我想一個快速的方法給我有超過100萬頁的文件。

感謝

+0

http://docs.mongodb.org/manual/reference/operator/query/regex/ – Disposer

+1

老實說,閱讀文檔可能會有幫助。您可以在該字段上使用[regexes](http://php.net/manual/en/class.mongoregex.php)或[創建全文搜索索引](http://docs.mongodb.org/manual /型芯/索引文本/)。在此之前,您可能應該閱讀[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)和ESR的[如何問問題的智能方式](http:// catb.org/~esr/faqs/smart-questions.html)。 –

回答

1

我希望這會幫助你:

$query = "john"; 
$where = array(array('fullname' => array('$regex' => new MongoRegex("/$query/i")))); 
$mongoClient = new MongoClient(); 
$db = $mongoClient->selectDB("datbase_name"); 
$collection = $db->selectCollection("collection_name"); 
$cursor = $collection->find($where); 
相關問題