Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Latest commit 93d46bd Apr 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Fix uppercase of maxTimeMS Apr 2, 2019
test Fix uppercase of maxTimeMS Apr 2, 2019
.eslintrc Get toJavascriptString() working nicely Apr 3, 2017
.gitignore Use safer eval instead of sandbox Feb 5, 2019
.jsfmtrc Initial commit Apr 3, 2017
.npmignore Initial commit Apr 3, 2017
.travis.yml Initial commit Apr 3, 2017
LICENSE Initial commit Apr 3, 2017
README.md 📝 Update examples in README May 16, 2018
index.js Initial commit Apr 3, 2017
package-lock.json 1.3.4 Apr 15, 2019
package.json 1.3.4 Apr 15, 2019

README.md

mongodb-query-parser travis npm

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 parser = require('mongodb-query-parser');
var EJSON = require('mongodb-extended-json');
var queryAsAnObjectWithTypes = parser.parseFilter(query);

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

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

License

Apache 2.0

You can’t perform that action at this time.