This project is a MQL (MongoDB Query Language) compiler. Filter, Update, and Aggregation queries are supported. See the support table table for more info.
This project can be useful to mock some basic functionality of MongoDB's driver or simply using its query syntax for object matching.
The project is written in TypeScript, and targets ES2023.
This is a pure ESM package.
import { ObjectId } from 'bson' // or 'mongodb'
import {
compileExpression,
compileMatch,
compilePipeline,
compileUpdate,
} from 'mql-match'
const documents = [
{
_id: new ObjectId('507f1f77bcf86cd799439011'),
value: 130,
},
{
_id: new ObjectId('507f191e810c19729de860ea'),
value: 42,
},
]
const match = compileMatch({
_id: new ObjectId('507f1f77bcf86cd799439011'),
})
// logs { _id: new ObjectId("507f1f77bcf86cd799439011"), value: 130 }
console.log(documents.find(match))
const update = compileUpdate({
$set: {
my: 'Pleasure',
},
})
const oldObject = { _id: 'my_doc' }
update(oldObject)
// logs { _id: 'my_doc', my: 'Pleasure' }
console.log(oldObject)
const map = compileExpression({
_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 = compilePipeline([
{
$match: {
value: 42,
},
},
])
// logs [{ _id: new ObjectId("507f191e810c19729de860ea"), value: 42 }]
console.log(aggregate(documents))-
$addFields -
$bucket -
$bucketAuto -
$changeStream -
$collStats -
$count -
$currentOp -
$densify -
$documents -
$facet -
$fill -
$geoNear -
$graphLookup -
$group -
$indexStats -
$limit -
$listLocalSessions -
$listSessions -
$lookup -
$match -
$merge -
$out -
$planCacheStats -
$project -
$redact -
$replaceRoot -
$replaceWith -
$sample -
$search -
$searchMeta -
$set -
$setWindowFields -
$skip -
$sort -
$sortByCount -
$unionWith -
$unset -
$unwind
-
$$NOW -
$$CLUSTER_TIME -
$$ROOT -
$$CURRENT -
$$REMOVE -
$$DESCEND -
$$PRUNE
Operators not listed here are currently not supported. Feel free to open an GitHub Issue if you need something in particular.
-
$abs -
$add -
$ceil -
$divide -
$exp -
$floor -
$ln -
$log -
$log10 -
$mod -
$multiply -
$pow -
$round -
$sqrt -
$subtract -
$trunc
-
$arrayElemAt -
$arrayToObject -
$concatArrays -
$filter -
$first -
$firstN -
$in -
$indexOfArray -
$isArray -
$last -
$lastN -
$map -
$maxN -
$minN -
$objectToArray -
$range -
$reduce -
$reverseArray -
$size -
$slice -
$sortArray -
$zip
-
$dateAdd -
$dateDiff -
$dateFromParts -
$dateFromString -
$dateSubtract -
$dateToParts -
$dateToString -
$dateTrunc -
$dayOfMonth -
$dayOfWeek -
$dayOfYear -
$hour -
$isoDayOfWeek -
$isoWeek -
$isoWeekYear -
$millisecond -
$minute -
$month -
$second -
$toDate -
$week -
$year
-
$literalNo validation.
-
$concat -
$dateFromString -
$dateToString -
$indexOfBytes -
$indexOfCP -
$ltrim -
$regexFind -
$regexFindAll -
$regexMatch -
$replaceOne -
$replaceAll -
$rtrim -
$split -
$strLenBytes -
$strLenCP -
$strcasecmp -
$substr -
$substrBytes -
$substrCP -
$toLower -
$toString -
$trim -
$toUpper
-
$sin -
$cos -
$tan -
$asin -
$acos -
$atan -
$atan2 -
$asinh -
$acosh -
$atanh -
$sinh -
$cosh -
$tanh -
$degreesToRadians -
$radiansToDegrees
-
$convert -
$isNumber -
$toBool -
$toDate -
$toDecimal -
$toDouble -
$toInt -
$toLong -
$toObjectId -
$toString -
$typeAdds"unknown"type.
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.
