Skip to content

weiliddat/mgq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mgq

codecov

MongoDB query as a predicate function.

This aims to be consistent with how MongoDB's matches documents. This includes traversal across nested dicts and lists, None and field-presence/absence handling.

JavaScript port of mgqpy.

import { Query } from "mgq";

const predicate = Query({ "foo.bar": { $gt: 1 } });

const inputs = [
  { foo: [{ bar: [1, 2] }] },
  { foo: { bar: 1 } },
  { foo: { bar: 2 } },
  { foo: null },
];

const filtered = inputs.filter(predicate.test);

assert.deepEqual(filtered, [{ foo: [{ bar: [1, 2] }] }, { foo: { bar: 2 } }]);

Supported operators

Comparison query operators

  • $eq
  • $eq (implicit), e.g. {"foo": None}
  • $ne
  • $gt
  • $gte
  • $lt
  • $lte
  • $in
  • $nin

Logical query operators

  • $and
  • $and (implicit), e.g. {"foo": 1, "bar": "baz"}
  • $or
  • $not
  • $nor

Evaluation query operators

  • $regex
  • $regex (implicit), e.g. {"foo": re.compile('^bar')}
  • $mod
  • $where

Array query operators

  • $all
  • $elemMatch
  • $size

About

mongo query as a JS predicate

Resources

License

Stars

Watchers

Forks

Packages

No packages published