MongoDB find on all properties of document

泪湿孤枕 提交于 2019-12-12 16:17:58

问题


How do I create a query in mongodb to filter by any property of the document.

I have a collection of objects as follows:

{
  "_id" : ...,
  "name" : "John",
  "city" : "Rio de Janeiro",
  "state" : "RJ",
  "address" : "Street 1, .... "
}

I have on my page a field that allows a user to retrieve pro records in any column. If it was MYSQL I find the names of all the columns in the table and create a clause 'OR' searching for 'LIKE' the value entered by the user.

Is there any easy way to do this with mongodb, without having to search for all the existing properties in the document?


回答1:


At this time, I don't believe this is supported: https://jira.mongodb.org/browse/SERVER-1248

Alternatively, you could use Elasticsearch with MongoDB: How to use Elasticsearch with MongoDB?.

Once you set it up, you could easily run a match query to search all fields: {"query":{'match':{'_all':your_query}}}



来源:https://stackoverflow.com/questions/27806571/mongodb-find-on-all-properties-of-document

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