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"}}'
Errors
Common errors & fixes
Cannot find module 'bson'
Peer dependency bson not installed or incompatible version.
fixRun: 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.
fixUse: 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()).
fixUse 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).
fixInstall: npm install mongodb-query-parser. For browser, use a bundler with ESM support.
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).