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

我与影子孤独终老i 提交于 2019-12-24 02:13:09

问题


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. 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.



来源:https://stackoverflow.com/questions/35205837/how-to-search-word-or-string-on-all-fields-in-document-with-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!