SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

Filter not return the docs #48

Closed marcoriesco closed 9 years ago

marcoriesco commented 9 years ago

if /api/posts?filter={'title': "Teste de postagem"} - Returned

if /api/posts?filter={'title': "Teste"} - Not returned anything

Why?

mkjsix commented 9 years ago

Because the db.collection.find() method in MongoDB performs an exact match. Have a look at: http://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like

marcoriesco commented 9 years ago

Before you go searching hours here .. with Restheart I can not do a search , for example a title , with the partial letters ? Only with the full title ?

ujibang commented 9 years ago

The filter query parameter is a mongodb query where you can use any mongodb operator.

In your case you can use the $regex operator:

/api/posts?filter={'title': {'$regex': '(?i)^Teste.*'}}

The i option performs a case-insensitive match for documents with title value that starts with "Test".

Of course, you can also build more complex queries, just for example:

/api/posts?filter={'$and': [{'title': {'$regex": "(?i)^Test.*'}, {'publish_date':{'$gte': {'$date': '2015-09-04T08:00:00Z' }}}]}

That selects documents with title starting with Test and published (assuming that field existing) starting from 4/9/2015, 8AM

ujibang commented 9 years ago

also refer to the documentation: query documents section

marcoriesco commented 9 years ago

Muito Obrigado!

Thanks very mutch :)