1.1.0 • Published 5 months ago
mgq v1.1.0
mgq
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
Array query operators
- \$all
- \$elemMatch
- \$size