mongo-query-parse-filter
v1.0.5
Published
Lightweight package that allows you to easily convert complex, human-readable query filters into MongoDB-compatible query syntax, supporting logical operations like `AND`, `OR`, `NOT`, `IN`, `NIN` and various comparison operators
Downloads
404
Maintainers
Readme
Mongo Query Parse Filter
Lightweight package that allows you to easily convert complex, human-readable query filters into MongoDB-compatible query syntax, supporting logical operations like AND
, OR
, NOT
, IN
, NIN
and various comparison operators
Table of Contents
Installation
To install the package, use npm:
npm i mongo-query-parse-filter
Usage
const { MongoQuery } = require('mongo-query-parse-filter');
const mongoQuery = new MongoQuery();
const query = mongoQuery.buildQuery('(email eq "[email protected]")')
console.log(query)
Result
{
"email": { "$eq": "[email protected]" }
}
Example
eq,neq,gt,gte,lt,lte,regex: (email regex "(?i)@example.com$")
{
"email": { "$regex": "(?i)@example.com$" }
}
OR/AND: (email eq "[email protected]") or (username eq "alice")
{
"$or": [
{
"email": { "$eq": "[email protected]" }
},
{
"username": { "$eq": "alice" }
}
]
}
NOT: (not ((department eq "Marketing") or (department eq "Sales")))
to find all employees who are not either in the Marketing or Sales departments
{
"$not": {
"$or": [
{
"department": { "$eq": "Marketing" }
},
{
"department": { "$eq": "Sales" }
}
]
}
}
IN/NIN: (email in "'[email protected]','[email protected]'")
{
"email": { "$in": [ "[email protected]", "[email protected]"] }
}