mongodb-language-model
Parses a MongoDB query and creates hierarchical Ampersand.js models for each of the language components, which can then be interacted with programmatically.
UML diagram
This is the hierarchical model that is created when a query is parsed:
Example
var Query = require('mongodb-language-model').Query;
var assert = require('assert');
var query = new Query(
{"foo": 12345, "$and": [ {bar: false}, {baz: "hello"} ] },
{parse: true}
);
assert.equal(query.clauses.length, 2);
var leafClause = query.clauses.get('foo');
assert.equal(leafClause.className, 'LeafClause');
assert.equal(leafClause.key.serialize(), 'foo');
assert.equal(leafClause.value.serialize(), 12345);
var exprTree = query.clauses.get('$and');
assert.equal(exprTree.className, 'ExpressionTree');
var keys = exprTree.expressions.map(function (expr) {
return expr.clauses.at(0).key.serialize();
});
assert.deepEqual(keys, ['bar', 'baz']);
var values = exprTree.expressions.map(function (expr) {
return expr.clauses.at(0).value.serialize();
})
assert.deepEqual(values, [false, 'hello']);
Installation
// @todo
npm install --save mongodb-language-model
Testing
npm test
License
Apache 2.0