Easy Query

- 1 min

Easy Query is small and simple DLS (domain specific language) library that turns human-readable query strings into Mongo selectors. It’s implemented in javascript and can run both in the browser and Node.

Why Another DSL?

For many years I’ve been maintaining a web app that lets users search amongst cards in a collectible card game. Originally you could only search for cards by their name but this was limiting and I wanted something more powerful.

I could have used more advanced boolean logic with terms being joined by binary operators, however I believe that many of my users aren’t familiar with boolean logic and in the end the syntax often ends up looking terrible.

Instead I wanted a simple language that I could explain with a couple of examples that was lightweight yet powerful enough to generate most basic Mongo selectors. Inspired by the Google’s advanced format I created Easy Query.

Example Syntax

The DSL contains keywords corresponding to fields in the Mongo collection. These could be used to search that field together with standard inequality operators. Imagine for example a car database, in the collection we have the fields “brand”, “model”, and “year”. The two former fields contain strings and the latter contains integers.

The user could write

brand: BMW model: "5-series" year:>2000

which would generate the Mongo selector

{$and: [{brand: "BMW"}, {brand: "5-series"}, {year: {$gt: 2000}}]}

The library has some more bells and whistles like settign the default field, aliases and “or”-operators but the example above shows the gist of it.

Trying Easy Query

If you want to check out the library it’s open source on my Github with instruction on how to configure the library.

Erik Gärtner

Erik Gärtner

Ph.D. student in Deep Learning and Computer Vision with an interest in entrepreneurship

rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora