I have a collection which contains only 1 document:
events: ['some event', 'another one', 'and another'];
how can I perform a search against this so if the search parameter was "som", the docs returned would be "some event".
Currently I have the following:
var collection = db.collection('suggestions');
collection.ensureIndex({events: "text"}, function(){});
collection.find({ $text: { $search: term } }).toArray(function(err, docs){
if(err){
console.log(err);
}else{
res.send(docs);
}
});
where term is the search term - but this always returns an empty array [].
Can this be done with mongoDB?
$textis a full-text search. It searches over the tokens in your input text (with word stemming, so that{$text: {$search: 'events'}}would actually find something, even if the word your document actually contains is'event'). It depends on the value oftermand the value ofdefault_languageof the index (or$languageparameter of the search) what the result will look like. Maybe you didn't actually want to use a$textsearch?