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")}');
Usage with codemirror
var parser = require('mongodb-query-parser');
var query = '{_id: ObjectId("58c33a794d08b991e3648fd2")}';
parser.detect(query);
var queryAsJSON = '{"_id":{"$oid":"58c33a794d08b991e3648fd2"}}';
parser.detect(queryAsJSON);
parser.toJavascriptString(parse(query));
Extended JSON Support
var parser = require('mongodb-query-parser');
var EJSON = require('mongodb-extended-json');
var queryAsAnObjectWithTypes = parser.parseFilter(query);
EJSON.stringify(queryAsAnObjectWithTypes);
var queryAsJSON = '{"_id":{"$oid":"58c33a794d08b991e3648fd2"}}';
parser.detect(queryAsJSON);
License
Apache 2.0