Mongodb partial matching
For full-text search, first you must create a Text Index on your nicknames
field. Documents inserted before an index has been created will not be searchable. The search only works for documents that have been inserted after the index has been created. Then when you perform a search using MongoDb's $text
and $search
operators, MongoDb will return the documents whose nicknames
field corresponds to the search text. For regex matching, MongoDb has a $regex
operator you can use.
Here are a couple of short examples:
Full Text Search
- Save this script as
football.js
. It will create ateams
collection with a Text Index and two documents for us to search.
// create football databasevar db = connect("localhost:27017/football");/* note: You may also create indexes from your console using the MongoDb shell. Actually each of these statements may be run from the shell. I'm using a script file for convenience.*/// create Text Index on the 'nicknames' field // so full-text search worksdb.teams.createIndex({"nicknames":"text"});// insert two teams to search fordb.teams.insert({ name: 'Real Madrir', nicknames: ['Real', 'Madrid', 'Real Madrir' ]})db.teams.insert({ name: 'Fake Madrir', nicknames: ['Fake']})
Open your terminal and navigate to the directory where you saved
football.js
, then run this script against your local MongoDb instance by typingmongo football.js
.Type
mongo
from your terminal to open the MongoDb Shell and switch to thefootball
database by typinguse football
.Once you're in the football database, search for one of your documents using
db.teams.find({"$text":{"$search":"<search-text>"}})
> use football// find Real Madrir> db.teams.find({"$text":{"$search":"Real"}})// find Fake Madrir> db.teams.find({"$text":{"$search":"Fake"}})
Regex
If you want to search using a regex, you will not need to create an index. Just search using mongodb's $regex
operator:
//find Real Madrirdb.teams.find({"nicknames": {"$regex": /Real/}})db.teams.find({"nicknames": {"$regex": /Real Madrir/}})//find Fake Madrirdb.teams.find({"nicknames": {"$regex": /Fa/}})db.teams.find({"nicknames": {"$regex": /ke/}})
Mongoose
This is how each of these searches would work in NodeJS using mongoose:
var searchText = "Madrir"; // or some value from request.bodyvar searchRegex = new RegExp(searchText);var fullTextSearchOptions = { "$text":{ "$search": searchText }};var regexSearchOptions = { "nicknames": { "$regex": searchRegex }};// full-text searchTeam.find(fullTextSearchOptions, function(err, teams){ if(err){ // ... }else if(teams){ // ... }})// regex searchTeam.find(regexSearchOptions, function(err, teams){ if(err){ // ... }else if(teams){ // ... }})