Mastodon hachyterm.io

Today I learned about using text search in MongoDB.

The easiest way is to define a field as “text”:

/* JavaScript example with Mongo Driver */
const result = await db
  .collection('books')
  .createIndex({ title: 'text', description: 'text' })

Here, the fields for title and description now have an index type of “text”.

Now you can use the $text query to find text inside those fields.

db.books.find({ $text: { $search: 'Oliver' } })

Further Reading