# @mongodb-js/shell-bson-parser

> Parse valid MongoDB shell syntax queries

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

## Install

```sh
npm install @mongodb-js/shell-bson-parser
pnpm add @mongodb-js/shell-bson-parser
yarn add @mongodb-js/shell-bson-parser
bun add @mongodb-js/shell-bson-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 | 1.5.19 |
| Published | 2026-09-09 |
| First published | 2024-07-09 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 60.2 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 |

## Links

- npm: https://www.npmjs.com/package/@mongodb-js/shell-bson-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-js/shell-bson-parser

## Dependencies (1)

- [acorn](https://npm.io/package/acorn.md) ^8.16.0

## Recent versions

- 1.5.19 (latest) — 2026-09-09
- 1.5.18 — 2026-09-09
- 1.5.17 — 2026-09-07
- 1.5.16 — 2026-09-07
- 1.5.15 — 2026-08-19
- 1.5.14 — 2026-07-24
- 1.5.13 — 2026-06-18
- 1.5.12 — 2026-05-03
- 1.5.11 — 2026-05-01
- 1.5.10 — 2026-04-23
- 1.5.9 — 2026-04-22
- 1.5.8 — 2026-04-16
- 1.5.7 — 2026-03-27
- 1.5.6 — 2026-02-11
- 1.5.5 — 2026-01-19
- … 16 more at https://npm.io/package/@mongodb-js/shell-bson-parser/versions

## README

# @mongodb-js/shell-bson-parser

Parses valid MongoDB Shell queries.
This library does not validate that these queries are correct. It's focus is on parsing untrusted input. You may wish to use something like https://github.com/mongodb-js/mongodb-language-model to achieve this.

This library creates an AST from the proposed input, and then traverses this AST to check if it looks like a valid MongoDB query. If it does, the library will then evaluate the code to produce the parsed query.

This library currently supports three different modes for parsing queries:

**strict**: [default] Disallows comments and calling methods

```javascript
import parse from '@mongodb-js/shell-bson-parser';

const query = parse(
  `{
    _id: ObjectID("132323"),
    simpleCalc: 6,
    date: new Date(1578974885017)
  }`,
  { mode: 'strict' },
);

/*
  query = { _id: ObjectID("132323"), simpleCalc: 6, date: Date('1578974885017') }
*/
```

**weak**: Disallows comments, allows calling methods

```javascript
import parse from '@mongodb-js/shell-bson-parser';

const query = parse(
  `{
    _id: ObjectID("132323"),
    simpleCalc: Math.max(1,2,3) * Math.min(4,3,2)
  }`,
  { mode: 'weak' },
);

/*
  query = { _id: ObjectID("132323"), simpleCalc: 6 }
*/
```

**loose**: Supports calling methods on Math, Date and ISODate, allows comments

```javascript
import parse from '@mongodb-js/shell-bson-parser';

const query = parse(
  `{
    _id: ObjectID("132323"), // a helpful comment
    simpleCalc: Math.max(1,2,3) * Math.min(4,3,2)
  }`,
  { mode: 'loose' },
);

/*
  query = { _id: ObjectID("132323"), simpleCalc: 6 }
*/
```

The options object passed into parse has the following parameters:

```javascript
{
  mode: ('loose' || 'weak' || 'strict') // Will assign (allowMethods & allowComments) for you
  allowMethods: true, // Allow function calls, ie Date.now(), Math.Max(), (new Date()).getFullYear()
  allowComments: true, // Allow comments (// and /* */)
}
```

The flags can be set to override the default value from a given mode, ie:

```javascript
{
  mode: 'strict',
  allowComments: true
}
```

This options object will disallow method calls, but will allow comments

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