
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
mongo-parse
Advanced tools
mongo-parse
A parser for mongo db queries and projections. You can use this to analyze, modify, and match against MongoDb queries, as well as test projections for inclusiveness or exclusiveness.
Example:
var parser = require('mongo-parse')
var ObjectId = require('mongodb').ObjectId
var query = parser.parse({ "powerlevel": { $gt: 9000 }})
// query.parts contains: [{field: 'powerlevel', operator: '$gt', operand: 9000}]
var query2 = {$and:[{userId: "507f191e810c19729de860ea"}, {animal: {$in: ['beefalo', 'deerclops']}}]}
var newQuery = parser.parse(query2).mapValues(function(field, stringId) {
if(field === 'userId')
return ObjectId(stringId) // change a string ID into an ObjectId when you need to
else
return stringId
})
// newQuery is {$and:[{userId: ObjectId("29g8j3h27fh382dh82ae23")}, {animal: {$in: ['beefalo', 'deerclops']}}]}
parser.parse(newQuery).matches({userId: ObjectId("507f191e810c19729de860ea"), animal: 'deerclops'}) // returns true
npm install mongo-parse
var parser = require('mongo-parse')
var queryObject = parser.parse(mongoQuery)
- Returns an object that contains a list of query parts, and methods for interacting with the query.
queryObject.parts
- A list of QueryPart objects.
queryObject.mapValues(function(field, value) {...})
- Returns a new mongo query object with values mapped based on the passed in
callback. The callback will be called for each leaf-node in the query.
For example, in the query {x:1, $and:[{y:2,z:3}]}
, the callback will be called 3 times.
The value returned from the callback function will replace the original value in the new returned query.
Query parts that don't relate to a field may not trigger the callback. The callback's parameters:
field
- The field the query part is for. E.g. for {x:1}
, the field will be "x"
. Can be undefined
for certain query parts that don't relate to a specific field (e.g. the $text
operator).value
- The value that query part is querying with. E.g. for {x:1}
, the value will be 1
.queryObject.map(function(field, value) {...})
- Returns a new mongo query object with query parts mapped based on the
return value of the passed-in callback.
The callback will be called for each leaf-node in the query.
For example, in the query {x:1, $and:[{y:2,z:3}]}
, the callback will be called 3 times.
If the value returned from the callback is null
, the original query part will be removed.
If the value returned from the callback is undefined
, the original query part will be kept.
Otherwise, the query part will be replaced with the query parts contained in the returned query object.
Query parts that don't relate to a field may not trigger the callback. The callback's parameters:
field
- The field the query part is for. E.g. for {x:1}
, the field will be "x"
. Can be undefined
for certain query parts that don't relate to a specific field (e.g. the $text
operator).value
- The value that query part is querying with. E.g. for {x:1}
, the value will be 1
.queryObject.matches(document, validate)
- Returns true if the query matches the passed mongodb document
object. The following mongo operators are supported: basic equality ({field:value}), $gt, $gte, $lt, $lte, $ne, $in, $nin, $all, $mod, $exists, $regex, $size, $elemMatch, $not, $and, $or, $nor, $where (and implicit where - passing a function), $comment. The following mongo operators are not yet supported $geoIntersects, $geoWithin, $nearSphere, $near, $text, projection operators ($, $meta, $slice)
validate
- (Optional - Default: true) Whether to validate that the passed document is a correctly structured mongo document or not.parser.search(documents, query, sort, validate)
- Returns the list of matching documents
sorted.
documents
- The array of documents to search.query
- The mongo query to search with.sort
- (Optional) A mongo sort definition to sort by.validate
- (Optional - Default: true) Whether to validate that the passed document is a correctly structured mongo document or not.parser.inclusive(mongoProjection)
- Returns true
if the projection is inclusive, false
if it is exclusive, and undefined
if it is neither. If it is neither, you may either add more exclusive terms or more inclusive terms. Note that fields using the $elemMatch
or $slice
projection operators can be used with both inclusive and exclusive queries and so have no bearing on inclusiveness. See here for more info on projections.
parser.compressQuery(query)
- Returns the same query but shortened, but collapsing $ands, $ors, and $eqs.
A QueryPart contains the following properties:
field
- The field a query part relates to. Can be undefined
if the queryPart doesn't related to a specific field.operator
- The operator of a query part.operand
- The operand for a query part. This is the whole value or object contained for the given operation. For example, for {x: 2}
the operand is 2
, for {x: {$lt:3}}
the operand is {$lt:3}
, and for {$and:[{x:1},{y:2}]}, the operand is [{x:1},{y:2}]
.parts
- A list of QueryPart for parts contained within the given query part. For example, for {a:{$not:{$lt: 4}}}
the parts contains the $lt operator, for {$and:[{x:1},{y:2}]}
there are two elements in parts
: one for {x:1}
and one for {y:2}
.implicitField
- If false, it means that the parts
of this $elemMatch query part contains normal query parts. If true, it means that the parts
of this $elemMatch query part contains field operators (like $gt or $in) that will have undefined
field
properties. implicitField
will be undefined
for any QueryPart object who's operator
is not "$elemMatch"
.var pointers = parser.DotNotationPointers(rootObject, field)
- A function that returns a list of DotNotationPointer objects, which allow you to get and set a nested property inside a mongo document object using dot notation.
rootObject
- an object that may have the given field
field
- a fieldname, which can be expressed in dot notation (e.g. 'x' and 'x.y.0.z' are both valid for field
)Note that this returns a list because a single field
path can map to many actual properties because of how mongo fans out the matching paths for arrays. For example, DotNotationPointers({a:[{b:1},{b:2}]},"a.b")
will return two pointers, one pointing to "a.0.b" and one pointing to "a.1.b".
A pointer that can get and set a nested property within a mongo document object using dot notation. The object has the following properties:
pointer.val
- a getter/setter value that can be used to both get and set the value selected by the field
passed into the DotNotationPointers
function. Setting the field to undefined
will delete
it from the object.
pointer.property
- an array representing the field
, split by '.'. For example, for 'a'
this will hold ['a']
, and for 'a.b'
this will hold ['a','b']
.
pointer.propertyInfo
- an object with the following properties:
obj
- an object reference for use in getting or setting the value pointed to.last
- the property within obj
that holds the value pointed to.search
methodnull
validate
parameter wasn't being respected in certain cases (Thanks jgpacheco!)map
was treating $or like $andDotNotationPointer.val = undefined
now deletes the propertysearch
and matches
search
methodeval
to using the more isolated new Function
inclusive
inclusive
method.Anything helps:
How to submit pull requests:
npm install
at its rootReleased under the MIT license: http://opensource.org/licenses/MIT
FAQs
A parser for mongo db queries
The npm package mongo-parse receives a total of 1,554 weekly downloads. As such, mongo-parse popularity was classified as popular.
We found that mongo-parse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.