mongodb-query-parser
TypeScript icon, indicating that this package has built-in type declarations

4.3.2 • Public • Published

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:

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

Usage with codemirror

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

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 which covers what to look out for between releases.

Related

License

Apache 2.0

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.3.2932latest

Version History

VersionDownloads (Last 7 Days)Published
4.3.2932
4.3.14
4.3.042,520
4.2.6216
4.2.54
4.2.44
4.2.36
4.2.23
4.2.17
4.2.02
4.1.32
4.1.23
4.1.12
4.1.03
4.0.22
4.0.01,203
3.1.3187
3.1.22
3.1.12
3.1.02
2.5.01,998
2.4.1157
2.4.103
2.4.93
2.4.82
2.4.72
2.4.6131
2.4.52
2.4.46
2.4.33
2.4.23
2.4.12
2.4.03
2.3.02
2.2.02
2.1.220
2.1.17
2.1.02
2.0.24
2.0.14
2.0.03
1.6.0-rc.02

Package Sidebar

Install

npm i mongodb-query-parser

Weekly Downloads

37,214

Version

4.3.2

License

Apache-2.0

Unpacked Size

48.9 kB

Total Files

16

Last publish

Collaborators

  • mongodb-js-user
  • rueckstiess
  • durran
  • alexander_schroll
  • mbroadst
  • hswolff
  • satyasinha
  • matt_d_rat
  • jeff-allen-mongo
  • jonathan.balsano
  • mongodb-build
  • jack.weir
  • mcasimir
  • kristina.stefano
  • jarjee
  • shaketbaby
  • devtoolsbot
  • addaleax
  • gribnoysup
  • mutukrish
  • mongo-j
  • nbbeeken
  • dbx-node
  • nirinchev
  • pearsb1
  • imlucas