Reputation: 577
I have a collection like
{
City : "CityName11",
State : "StateName1"
},
{
City : "CityName2",
State : "StateName2"
},{
City : "CityName3",
State : "StateName3"
}
Now on City field I have text index, I want to search using "ci"
, and it should return all result which contains that phrase. I have tried with $search: "\"Ci\""
and seems that it is not working. Any help will be appreciated.
Upvotes: 0
Views: 389
Reputation: 50426
Text indexes do not work very well ( basically they do not work at all ) with things that are not actually "whole words".
For you type of serach you need to use a $regex
instead:
db.collection.find({ "City": { "$regex": "ci", "$options": "i" } })
And prefereably anchored to the start of the string and without the "case insensitive" option, which allows the use of an index ( a regular index ):
db.collection.find({ "City": { "$regex": "^Ci" } })
Upvotes: 1