mql-match
TypeScript icon, indicating that this package has built-in type declarations

0.7.1 • Public • Published

mql-match

NPM Version NPM Dependency status NPM Downloads JavaScript Style Guide GitHub last commit

This project is a MQL (MongoDB Query Language) compiler. Filter, Update, and Aggregation queries are supported. See the support table table for more info.

Why

This project can be useful to mock some basic functionality of MongoDB's driver or simply using its query syntax for object matching.

Example

import { ObjectId } from 'bson' // or 'mongodb'
import {
  compileAggregationExpression,
  compileAggregationPipeline,
  compileFilterQuery,
  compileUpdateQuery
} from 'mql-match'

const documents = [
  {
    _id: new ObjectId("507f1f77bcf86cd799439011"),
    value: 130
  },
  {
    _id: new ObjectId("507f191e810c19729de860ea"),
    value: 42
  }
]

const match = compileFilterQuery({
  _id: new ObjectId("507f1f77bcf86cd799439011")
})

// logs { _id: new ObjectId("507f1f77bcf86cd799439011"), value: 130 }
console.log(documents.find(match))

const update = compileUpdateQuery({
  $setOnInsert: {
    hello: 'World'
  },
  $set: {
    my: 'Pleasure'
  }
})

const oldObject = { _id: "my_doc" }
update(oldObject)
// logs { _id: 'my_doc', my: 'Pleasure' }
console.log(oldObject)

const newObject = {}
// the `true` say that this document was inserted
update(newObject, true)
// logs { _id: new ObjectId("xxxxxxxxxxxxxxxxxxxxxxxx"), hello: 'World', my: 'Pleasure' }
console.log(newObject)

const map = compileAggregationExpression({
  _id: 0,
  item: 1,
  discount: {
    $cond: {
      if: { $gte: ['$qty', 250] },
      then: 30,
      else: 20
    }
  }
})

// logs { item: 'xyz1', discount: 30 }
console.log(map({ _id: 3, item: 'xyz1', qty: 250 }))

// Returns a function that accepts an iterable (both sync or async) and returns an async iterable
const aggregate = compileAggregationPipeline([
  {
    $match: {
      value: 42
    }
  }
])

async function pipelineExample () {
  // logs { _id: new ObjectId("507f191e810c19729de860ea"), value: 42 }
  for await (const document of aggregate(documents)) {
    console.log(document)
  }
}

pipelineExample().catch(err => console.error(err))

Supported features

Query Operators

Comparison

Logical

Element

Evaluation

Geospatial

Array

Bitwise

Projection

Miscellaneous

Update Operators

Fields

Array

Modifiers

Bitwise

Aggregation Pipeline Stages

Aggregation Variables

  • [x] $$NOW
  • [x] $$CLUSTER_TIME
  • [x] $$ROOT
  • [ ] $$CURRENT
  • [ ] $$REMOVE
  • [ ] $$DESCEND
  • [ ] $$PRUNE

Aggregation Pipeline Operators

Operators not listed here are currently not supported. Feel free to open an GitHub Issue if you need something in particular.

Arithmetic Expression Operators

Array Expression Operators

Boolean Expression Operators

Comparison Expression Operators

Conditional Expression Operators

Custom Aggregation Expression Operators

Data Size Operators

Date Expression Operators

Literal Expression Operator

Miscellaneous Operators

Object Expression Operators

Set Expression Operators

String Expression Operators

Text Expression Operator

Timestamp Expression Operators

Trigonometry Expression Operators

Type Expression Operators

Support

If this library helps you in your organization, you can show some love by giving the repo a star or support by making a nominal monetary contribution.

"Buy Me A Coffee"

Package Sidebar

Install

npm i mql-match

Weekly Downloads

10

Version

0.7.1

License

MIT

Unpacked Size

167 kB

Total Files

53

Last publish

Collaborators

  • greguz