How to search word or string on all fields in document with MongoDB? How to search word or string on all fields in document with MongoDB? mongodb mongodb

How to search word or string on all fields in document with MongoDB?


You need to create a wildcard text indexes like this:

db.collection.createIndex( { "$**": "text" } )

You can the use the $text operator to performs a text search.

As mentioned in the documentation:

This index allows for text search on all fields with string content. Such an index can be useful with highly unstructured data if it is unclear which fields to include in the text index or for ad-hoc querying.