Db.find sort/search for text


I have this code.

    "submit .new-task": function (event) {
      // Prevent default browser form submit
      // Get value from form element
      var text = event.target.text.value;
      // Insert a task into the collection
        text: text,
        createdAt: new Date() // current time
      // Clear form
      event.target.text.value = "";

it works great, it populates my todo list with events as expected.
Now i want to make a sort function that shows only tasks that have cucumber in it and i dont get it to work.
I have read alot of different posts about it but still no luck as they always speak about old mongoDB etc.

I can sort the list by date created without any issues.

      return EventDB.find({}, {sort: {createdAt: -1}});

But now i want to try to sort it by text, i have tried for example

         return EventDB.find({$text:{$search:"Cucumber"}});

But it removes everything on my list instead of only showing events with Cucumber in the text.
I have read something about that i need to index the DB but when i try that i get compilation errors instead.
What would be the correct way?

Can i get some help how to make a sort by text search?

Im following the todo list tutorials for your refference.


Do you want to do search feature or just query and return value ?

I just need to return the query, i have a checkbox and when its checked only the events that have cucumber in it should show. When i untick the checkbox all event should be shown.



EventDB.find({ text: /Cucumber/ });

This will return all matches with “Cucumber” anywhere in the text field.

1 Like

That was the one :slight_smile:
Thank you very much…