Registry / database / mongodb-language-model

mongodb-language-model

JSON →
library2.3.0jsnpmunverified

Parses MongoDB query language strings into an abstract syntax tree (AST) with part-of-speech tagging. Version 2.3.0 is the current stable release, with infrequent updates. Supports strict extended JSON syntax only. Differentiators: provides both acceptance validation and detailed AST generation, designed for use in MongoDB Compass.

npm install mongodb-language-model
INSTALL
IMPORT
SIG · MONGODB-LANGUAGE-M
M
mongodb-language-model
databasejavascriptv2.3.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

accepts
import { accepts } from 'mongodb-language-model'
const { accepts } = require('mongodb-language-model')
Both ESM and CJS imports work; the package is used in both Node.js and browser environments.
parse
import { parse } from 'mongodb-language-model'
const parse = require('mongodb-language-model').parse
parse throws pegjs.SyntaxError on invalid input.
default import
import mongoLM from 'mongodb-language-model'
const mongoLM = require('mongodb-language-model')
Default import provides object with accepts and parse functions.

Shows how to use accepts and parse functions for MongoDB query strings.

import { accepts, parse } from 'mongodb-language-model'; const validQuery = '{"name": "Alice"}'; const invalidQuery = '{"$foo": "bar"}'; console.log(accepts(validQuery)); // true console.log(accepts(invalidQuery)); // false try { const ast = parse(validQuery); console.log(JSON.stringify(ast, null, 2)); } catch (err) { console.error('Parse error:', err.message); }
Debug
Known issues
gotchaOnly strict extended JSON syntax is supported; keys must be double-quoted and values must follow MongoDB extended JSON strict format.
fix
Ensure query string uses double quotes for keys and strict extended JSON for values.
affects: >0
deprecatedThe package may not receive frequent updates; consider using `mongodb-query-parser` or native MongoDB driver parsing for more features.
fix
Evaluate if alternative packages better suit your needs.
affects: >0
gotchaparse() throws a pegjs.SyntaxError on invalid queries; this is not a standard Error type.
fix
Catch errors with (err instanceof pegjs.SyntaxError) or check with accepts() before parsing.
affects: >0
gotchaThe AST structure uses part-of-speech tags like 'pos' which may change between minor versions without notice.
fix
Do not rely on exact AST shape; validate with unit tests after upgrades.
affects: >0
Errors
Common errors & fixes
SyntaxError: Expected "{" but end of input found.
Query string is empty or not valid JSON due to missing braces.
fix
Ensure the query string is a valid JSON object, e.g., '{"key": "value"}'.
TypeError: accepts is not a function
Incorrect import style; likely used default import instead of named import.
fix
Use import { accepts } from 'mongodb-language-model' or const { accepts } = require('mongodb-language-model').
pegjs.SyntaxError: Expected [\-] or [0-9] but "a" found.
Invalid numeric value or missing quotes around string value in extended JSON.
fix
Follow strict extended JSON format; strings must be quoted, numbers are plain.
ReferenceError: pegjs is not defined
The pegjs dependency is not installed; not caught in try-catch.
fix
Install pegjs: npm install pegjs
Upgrade
Version history
2.3.0latest on npm
Audit
Dependencies
pegjsrequiredUsed for parsing grammar
Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
mongodb-language-model — npm install mongodb-language-model · libregistry