MongoDB text search separators
I am trying to find mongoDB text search on mongoDB server version 2.6.3. When I do a text search with the below query
db.collection.find({"$text" : {"$search" : "a@b"}})
I am getting as result documents containing a or b, which means I am getting results for
db.collection.find({"$text" : {"$search" : "a b"}})
My guess is that the mongo text parser treats the character '@'
as a delimiter and replaces it with a space. Is it possible to specify that the character '@'
should not be treated as a delimiter?
source to share
In phrase matching , as opposed to individual terms, enclose the phrase in escaped double quotes , as in: (\")
"\"a@b\""
So your request will look like this:
db.collection.find({"$text" : {"$search" : "\"a@b\""}})
For example:
db.collection.insert([
{"x": "a@b"},
{"x": "a b"},
{"x": "b"},
{"x": "a"}
]);
db.collection.createIndex({ x: "text"});
The request db.collection.find({"$text" : {"$search" : "\"a@b\""}})
returns
/* 0 */
{
"_id" : ObjectId("5549ddce180e849972939043"),
"x" : "a@b"
}
source to share