Pumych
Pumych

Reputation: 1398

Meteor.publish() - how to get documents by free text

I trying to get documents by free text. This part, that getting data by field works OK and returns data:

Meteor.publish("messages", function(){
    return Messages.find({ discussion_id: "discus_id_87" });
});

This one doesn't work:

Meteor.publish("messages", function(){
    return Messages.find({ $text: { $search: "Some text" } });
});

Returns an ERROR:

 Error: Exception while polling query {"collectionName":"messages","selector":{"$text":{"$search":"Some text"}},"options":{"transform":null}}: Unable to execute query: error processing query: ns=meteor.messages limit=0 skip=0

This is an example from mongoDB $text

db.articles.find( { $text: { $search: "bake coffee -cake" } } )

What I'm doing wrong? How to get documents by free text?

Thanks

Upvotes: 0

Views: 73

Answers (1)

Tarang
Tarang

Reputation: 75945

Make sure your mongo database is version 2.6. This is a relatively new MongoDB feature.

Also, you are required to create an index:

Meteor.startup(function (){
    Messages._ensureIndex({"$**": "text"}, {"name": "searchIndex"}); 
});

More info here: http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/

Upvotes: 3

Related Questions