Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oakfang/monjo
mongoDB inspired JSON filters for JS
https://github.com/oakfang/monjo
Last synced: 2 months ago
JSON representation
mongoDB inspired JSON filters for JS
- Host: GitHub
- URL: https://github.com/oakfang/monjo
- Owner: oakfang
- License: mit
- Created: 2019-02-15T21:48:48.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-08-10T22:35:47.000Z (over 3 years ago)
- Last Synced: 2024-08-08T15:17:52.895Z (5 months ago)
- Language: JavaScript
- Size: 188 KB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# monjo
mongoDB inspired JSON filters for JS
## Usage
```js
const compileFilter = require('monjo');
const query = {
name: 'Foo Bar',
age: {
$gt: 5,
},
hobbies: {
$elemMatch: {
$not: {
name: {
$eq: 'music',
},
},
},
},
};
const filter = compileFilter(query);
const users = getUsers();
for (let validUser of users.filter(filter)) {
// do stuff
}
```## Supported Operators
- `$eq` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/eq/).
- `$ne` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/ne/).
- `$gt` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/gt/).
- `$gte` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/gte/).
- `$lt` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/lt/).
- `$lte` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/lte/).
- `$in` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/in/).
- `$nin` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/nin/).
- `$not` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/not/).
- `$and` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/and/).
- `$or` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/or/).
- `$exists` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/exists/).
- `$size` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/size/).
- `$all` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/all/).
- `$elemMatch` - See mongoDB's [documentation](https://docs.mongodb.com/manual/reference/operator/query/elemMatch/).
- `$startsWith` - wraps over `String.prototype.startsWith`.
- `$endsWith` - wraps over `String.prototype.endsWith`.## Custom filters
Filters (nested, or otherwise) may be funtions that accept values and return a boolean-like value.
```js
const compileFilter = require('monjo');
const query = {
age(age) {
return age % 10 === 0;
},
};
const filter = compileFilter(query);
const users = getUsers();
for (let validUser of users.filter(filter)) {
// do stuff
}
```