Registry / database / mongodb-query-parser

mongodb-query-parser

JSON →
library4.7.12jsnpmunverified

Safely parse and validate MongoDB query strings (filters, projections, sort, etc.) into BSON-typed JavaScript objects. Current stable version is 4.7.12. Released as part of MongoDB DevTools Shared, with active maintenance and minor releases as needed. Supports ESM and CJS, ships TypeScript types, and integrates with Codemirror for syntax highlighting detection. Key differentiator: lossless BSON type handling (e.g., ObjectId, ISODate) from string input, unlike naive JSON.parse.

npm install mongodb-query-parser
INSTALL
IMPORT
SIG · MONGODB-QUERY-PARS
M
mongodb-query-parser
databasejavascriptv4.7.12
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.

default
import parse from 'mongodb-query-parser'
const parser = require('mongodb-query-parser')
Default export is a function that parses a query string and returns a parsed object or a ParseError. CommonJS require works but ESM import is preferred for tree-shaking.
parseFilter
import { parseFilter } from 'mongodb-query-parser'
const { parseFilter } = require('mongodb-query-parser')
Named export for parsing filter strings only. Equivalent to default export but more explicit. Available as named export since v4.
toJavascriptString
import { toJavascriptString } from 'mongodb-query-parser'
import toJavascriptString from 'mongodb-query-parser'
Named export, not default. Common mistake is to attempt default import.
ParseError
import { ParseError } from 'mongodb-query-parser'
Error class thrown on parse failure. TypeScript type exported.

Parses a MongoDB query string with ObjectId, detects syntax mode, and converts to JavaScript string and extended JSON.

import parse, { parseFilter, toJavascriptString, detect } from 'mongodb-query-parser'; import { ObjectId } from 'bson'; // Parse a MongoDB query string const query = '{_id: ObjectId("58c33a794d08b991e3648fd2")}'; const parsed = parse(query); if (parsed instanceof Error) { console.error('Parse error:', parsed.message); } else { console.log('Parsed:', parsed); // { _id: ObjectId('58c33a794d08b991e3648fd2') } } // Parse only a filter (equivalent to default) const filter = parseFilter('{ status: "active" }'); // Detect syntax highlighting mode for Codemirror console.log(detect(query)); // 'javascript' console.log(detect('{"$oid":"58c33a794d08b991e3648fd2"}')); // 'json' // Convert parsed object back to JavaScript string console.log(toJavascriptString(parsed)); // '{_id:ObjectId(\'58c33a794d08b991e3648fd2\')}' // Use extended JSON to serialize with BSON types import { EJSON } from 'bson'; console.log(EJSON.stringify(parsed)); // '{"_id":{"$oid":"58c33a794d08b991e3648fd2"}}'
Debug
Known issues
breakingPeer dependency bson must be manually installed and compatible. The package does not include bson itself.
fix
Install bson separately: npm install bson@^7.0.0 (or compatible version as per peerDependencies).
affects: >=4.0.0
breakingparse() returns a ParseError object on failure, not null or throws. Check instanceof Error.
fix
Always check if parse() result is an Error before using it: const result = parse(str); if (result instanceof Error) { ... }
affects: >=4.0.0
deprecatedrequire('mongodb-query-parser') as a function in CommonJS may not work in strict ESM environments.
fix
Use ESM import syntax or dynamic import() if available.
affects: >=4.0.0
gotchaThe package does not validate MongoDB operator syntax; it only parses the JavaScript/JSON string with BSON support. Invalid queries may parse to unexpected objects.
fix
Validate query structure separately with MongoDB server or a schema validator.
affects: *
gotchaBe careful with ObjectId string format - must be 24 hex characters. parse('{_id: ObjectId("invalid")}') will throw a ParseError.
fix
Ensure ObjectId strings are valid 24-character hex strings.
affects: *
Errors
Common errors & fixes
Cannot find module 'bson'
Peer dependency bson not installed or incompatible version.
fix
Run: npm install bson@^7.0.0 (or compatible version as per peerDependencies).
TypeError: parseFilter is not a function
Using named import from default export or incorrect import syntax.
fix
Use: import { parseFilter } from 'mongodb-query-parser'
Unexpected token o in JSON at position 1
Attempting to JSON.parse a MongoDB query string that is not valid JSON (e.g., contains ObjectId()).
fix
Use mongodb-query-parser's parse() function instead of JSON.parse().
Error: Cannot find module 'mongodb-query-parser'
Package not installed or wrong environment (e.g., browser).
fix
Install: npm install mongodb-query-parser. For browser, use a bundler with ESM support.
Upgrade
Version history
4.7.12latest on npm
Audit
Dependencies
bsonrequiredPeer dependency for BSON type constructors (ObjectId, ISODate, etc.) used during parsing. Must be compatible version (^4.6.3 || ^5 || ^6.10.3 || ^7.0.0).
Agent activity
8 hits · last 30 days
node
8
Resources
mongodb-query-parser — npm install mongodb-query-parser · libregistry