在mongodb中搜索全文

db.collection.createIndex({"type_name":"text"});
db.collection.find();

Outputs:

{ "type_id" : "10735", "type_name" : "Tycho" }

{ "type_id" : "1550", "type_name" : "Trina" }

{ "type_id" : "12925", "type_name" : "Tiffany" }

{ "type_id" : "1637", "type_name" : "The Wombats" }

{ "type_id" : "11470", "type_name" : "The Wanted" }

Using:

db.collection.find({'$text':{'$search':"The Wombats"}})

Outputs:

{ "type_id" : "1637", "type_name" : "The Wombats" }

Using:

db.collection.find({'$text':{'$search':"Wombats"}});

Outputs:

{ "type_id" : "1637", "type_name" : "The Wombats" }

But when I try :

db.collection.find({'$text':{'$search':"The"}});

Output:

0

I am expecting a different output, what is wrong?

Mongodb has some stop words for $text operator.

Stop Words

The $text operator ignores language-specific stop words, such as the and and in English.

Since you are using one of the stop word, in the query db.collection.find({'$text':{'$search':"The"}}) and hence it is not returning any results.

References:

https://docs.mongodb.com/v3.2/reference/operator/query/text/#op._S_text

https://docs.mongodb.com/v3.2/reference/operator/query/text/#match-operation

But when i am try this

$regex = new MongoRegex("/^".the."^/"."/i");

$artistNmaeRenge=array('type_name'=> $regex); it's working