Lucene Mongo Query
Lucene-inspired string-based mongodb query language for humans (and ferrets).
Installation
$ npm install lucene-mongo-query
Why?
- Nicer UX for the odd search / log filtering
- Writing JSON queries is a PITA
Example
var compile = require('monquery');
var str = 'level:error OR type:upload';
var query = compile(str);
Querying
Fields
Specify field names with optional values:
level: error;
yields
{
level: 'error';
}
Booleans
Omit value to imply true:
failed;
yields
{
failed: true;
}
Or specify a boolean-ish value (true, false, yes, no):
failed: no;
yields
{
failed: false;
}
Datetime
Specify field with datetime (ISO-8601) value:
lastmodified < '2020-10-06T18:43:26.000Z'
ObjectId
Specify field with $oid json notation value:
companies.id = {"$oid": "60b8f7f90f1aac77ed9db8c1"}
Operators
Currently supports AND / OR, which may be nested:
(level:error AND type:"upload failed") OR user.name.first:Tobi
yields
{ '$or':
[ { '$and': [ { level: 'error' }, { type: 'upload failed' } ] },
{ 'user.name.first': 'Tobi' } ] }
Regular Expressions
Regexps may be used with the //
syntax:
level:info AND name:/^To/
yields
{ '$and': [ { level: 'info' }, { name: /^To/ } ] }
case insensitive
level:info AND name:/^To/i
{ '$and': [ { level: 'info' }, { name: /^To/i } ] }
Create query for mongo variable ($$nameOfVariable)
To generate query with variable in instead of field may be used with the $$
syntax:
$$companies.name:/^name.*/i
yields
{ $regexMatch: {
input: '$$companies.name',
regex: /^name.*/i,
}
Patterns
Wildcards may be used to generate regular expressions:
level:error AND hostname:api-*
yields
{ '$and': [ { level: 'error' }, { hostname: /^api-.*$/ } ] }
License
MIT