Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB find on all properties of document

Tags:

mongodb

nosql

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?

like image 751
Leonardo Delfino Avatar asked Oct 22 '25 17:10

Leonardo Delfino


1 Answers

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

like image 104
datasci Avatar answered Oct 24 '25 20:10

datasci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!