# mongodb-query-parser

> Parse MongoDB queries

Latest version **5.0.5** (published 2026-09-09) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install mongodb-query-parser
pnpm add mongodb-query-parser
yarn add mongodb-query-parser
bun add mongodb-query-parser
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.5 |
| Published | 2026-09-09 |
| First published | 2017-05-31 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 49.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 24 |
| Author | MongoDB Inc |
| Maintainers | satyasinha, kristina.stefano, jarjee, devtoolsbot, mongo-j, nbbeeken, dbx-node, devtools-npm-user, imlucas |

## Links

- npm: https://www.npmjs.com/package/mongodb-query-parser
- Repository: https://github.com/mongodb-js/devtools-shared
- Issues: https://jira.mongodb.org/projects/COMPASS/issues
- npm.io page: https://npm.io/package/mongodb-query-parser

## Dependencies (4)

- [debug](https://npm.io/package/debug.md) ^4.4.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [javascript-stringify](https://npm.io/package/javascript-stringify.md) ^2.1.0
- [@mongodb-js/shell-bson-parser](https://npm.io/package/@mongodb-js/shell-bson-parser.md) ^1.5.19

## Recent versions

- 5.0.5 (latest) — 2026-09-09
- 5.0.4 — 2026-09-09
- 5.0.3 — 2026-09-07
- 5.0.2 — 2026-09-07
- 5.0.1 — 2026-08-19
- 5.0.0 — 2026-08-18
- 4.7.16 — 2026-08-05
- 4.7.15 — 2026-07-24
- 4.7.14 — 2026-06-30
- 4.7.13 — 2026-06-18
- 4.7.12 — 2026-05-03
- 4.7.11 — 2026-05-01
- 4.7.10 — 2026-04-23
- 4.7.9 — 2026-04-22
- 4.7.8 — 2026-04-16
- … 98 more at https://npm.io/package/mongodb-query-parser/versions

## README

# mongodb-query-parser

> Safe parsing and validation for MongoDB queries (filters), projections, and more.

## Example

Turn some JS code as a string into a real JS object safely and with no bson type loss:

```javascript
require('mongodb-query-parser')('{_id: ObjectId("58c33a794d08b991e3648fd2")}');
// >>> {_id: ObjectId('58c33a794d08b991e3648fd2'x)}
```

### Usage with codemirror

```javascript
var parser = require('mongodb-query-parser');
var query = '{_id: ObjectId("58c33a794d08b991e3648fd2")}';
// What is this highlighting/language mode for this string?
parser.detect(query);
// >>> `javascript`

var queryAsJSON = '{"_id":{"$oid":"58c33a794d08b991e3648fd2"}}';
// What is this highlighting/language mode for this string?
parser.detect(queryAsJSON);
// >>> `json`

// Turn it into a JS string that looks pretty in codemirror:
parser.toJavascriptString(parse(query));
// >>> '{_id:ObjectId(\'58c33a794d08b991e3648fd2\')}'
```

### Extended JSON Support

```javascript
var bson = require('bson');
var parser = require('mongodb-query-parser');
var queryAsAnObjectWithTypes = parser.parseFilter(query);

// Use extended json to prove types are intact
bson.EJSON.stringify(queryAsAnObjectWithTypes);
// >>> '{"_id":{"$oid":"58c33a794d08b991e3648fd2"}}'

var queryAsJSON = '{"_id":{"$oid":"58c33a794d08b991e3648fd2"}}';
parser.detect(queryAsJSON);
// >>> `json`
```

## Migrations

We aim to not have any API breaking changes in this library as we consider it stable, but breakages may occur whenever we upgrade a core dependency or perform a major refactor.

We have a [migration guide](MIGRATION.md) which covers what to look out for between releases.

## Related

- [`mongodb-stage-validator`](https://github.com/mongodb-js/stage-validator) Parse and validate MongoDB Aggregation Pipeline stages.
- [`@mongodb-js/compass-query-bar`](https://github.com/mongodb-js/compass-query-bar) Compass UI Plugin that uses `mongodb-query-parser` for validation.

## License

Apache 2.0

---
_Source: https://npm.io/package/mongodb-query-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
