How to search word or string on all fields in document with MongoDB?
问题 We have a situation where a user would like to have a single 'omni-box' for searching for a word\phrase anywhere in a document. Is MongoDB able to perform this search or would it have to be an explicit search of every field? 回答1: 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.