Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] Is it possible to filter with query other than equal? #953

Closed
joshuaavalon opened this issue Dec 11, 2018 · 2 comments
Closed

[Help] Is it possible to filter with query other than equal? #953

joshuaavalon opened this issue Dec 11, 2018 · 2 comments

Comments

@joshuaavalon
Copy link

joshuaavalon commented Dec 11, 2018

Is it possible to filter with query other than equal? For example, greater than, contains and other common query other than equal. There is no documentation on this. I wonder if it is possible.

@DavidB137
Copy link
Contributor

Hi!

Yes, it is possible. You can use "operators" or functions like:

  • $not or $ne: not equal to
  • $lt: less than
  • $lte: less than or equal to
  • $gt: greater than
  • $gte: greater than or equal to
    ...

See: /lib/MongoLite/Database.php

In action it looks like this:

https://example.com/admin/api/collections/get/animals/
   ?token=YOUR_SECRET_TOKEN
   &filter[age][$gte]=10

or you can combine them: e.g. to get only values in interval [8, 16):

https://example.com/admin/api/collections/get/animals/
   ?token=YOUR_SECRET_TOKEN
   &filter[age][$gte]=8
   &filter[age][$lt]=16

If you use JavaScript for accessing API:

fetch('/api/collections/get/animals/?token=YOUR_SECRET_TOKEN', {
   method: 'post',
   headers: { 'Content-Type': 'application/json' },
   body: JSON.stringify({
      filter: {
         age: {
            $gte: 8,
            $lt: 16
         }
      }
   })
})
.then(res => res.json())
.then(res => console.log(res));

@joshuaavalon
Copy link
Author

@DavidB137 Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants