0.0.4 • Published 6 years ago

mongo-explain-match v0.0.4

Weekly downloads
6
License
Apache-2.0
Repository
github
Last release
6 years ago

mongo-explain-match

(work in progress)

npm version Build Status

Utility library for explaining why a mongodb document matches a mongodb query.

Example

import { match } from 'mongo-explain-match';

const doc = {
  id: 1
};

const result = match({ id: { $in: [2, 3] } }, doc);

console.log(result);
// {
//   "match": true,
//   "reasons": [
//     {
//       "propertyPath": "id",
//       "queryPath": "id.$in",
//       "type": "IN_SET"
//     }
//   ]
// }

/**
 * can also only provide query to get curried matching function
 */
const docs = [
  { id: 1, name: 'Amanda' },
  { id: 2, name: 'Ben' },
  { id: 3, name: 'Chris' }
];

const filtered = docs.filter(
  match({
    $or: [{ name: /A/ }, { id: 2 }]
  })
);
// filtered === [
//   { id: 1, name: 'Amanda' },
//   { id: 2, name: 'Ben' },
// ];

Implemented query operators

Will Implement

  • $and
  • $nor
  • $or
  • $not
  • $nin
  • $in
  • $eq
  • $ne
  • $gt
  • $gte
  • $lt
  • $lte
  • $elemMatch
  • $size
  • $all
  • $exists

Won't Implement