1.2.1 • Published 6 years ago
monjo v1.2.1
monjo
mongoDB inspired JSON filters for JS
Usage
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.
- $ne- See mongoDB's documentation.
- $gt- See mongoDB's documentation.
- $gte- See mongoDB's documentation.
- $lt- See mongoDB's documentation.
- $lte- See mongoDB's documentation.
- $in- See mongoDB's documentation.
- $nin- See mongoDB's documentation.
- $not- See mongoDB's documentation.
- $and- See mongoDB's documentation.
- $or- See mongoDB's documentation.
- $exists- See mongoDB's documentation.
- $size- See mongoDB's documentation.
- $all- See mongoDB's documentation.
- $elemMatch- See mongoDB's documentation.
- $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.
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
}