问题
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