Full text search in MongoDB


I'm using a 'tags' collection to store tags. I want to search the tags to make suggestions for auto complete.

I have already applied this through the command line:

use databasename

db.tags.ensureIndex( { tag: "text" } )

Then I'm using this in PHP:

$tags = Tags::find(

'$text' => array('$search' => $tag),

'score' => array('$meta' => 'textScore')


But the result contains all the tags, even those who are not related. Please advise.



I hope to hear from @phalcon

edited Jul '16

it might be late but i finally figure out how to do full text search with mongodb

you will need to use $meta projection. somehow in phalcon they are named as a "fields" you can check it here https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/collection.zep#L387

Code billow will do full text search and order it by "textScore"

 $cursor = Tags::find([
                 '$text' => ['$search' => $Query]
             "fields" => ['score' => ['$meta' => "textScore"]],
             "sort" => ["score" => ['$meta' => "textScore"]]

@kenjilabs thanks for sharing!

edited Dec '16

To find based on mongodb full text search please don't forget to add the "conditions" keyword of phalcon

$cursor = Tags::find([ "conditions" => [ '$text' => ['$search' => $Query] ], "fields" => ['name'=>true, 'score' => ['$meta' => "textScore"]], "sort" => ["score" => ['$meta' => "textScore"]] ]);