Code Monkey home page Code Monkey logo

mongoql's Introduction

mongoql

Build Status

An OTP library to translate a special search query language (including aggregation) in MongoDB query.

Note: the queries are compatible with mongodb-erlang package.

Examples Query

E.g. select data where temperature>23 AND house is in Milano and pression <= 1015, ordered by house name ascending:

house.temperature>23 house.city:"Milano" house.pression<:1015 house.when>2017-12-15T10:20:00Z house.name-asc

Translated in the follow MongoDB query:

Query = {
  '$query', {
    '$and', [
      {<<"house.temperature">>, {'$gt', 23}},
      {<<"house.city">>, {'$eq', <<"Milano">>}},
      {<<"house.pression">>, {'$lte', 1015}},
      {<<"house.when">>, {'$gt', {1513, 333200, 0}}}
    ]
  },
  '$orderby', [
    {<<"house.name">>, 1}
  ]
},
mongopool_app:find(Pool, Table, Query).

Example Aggregation

E.g. count how many times received a new log message today grouped by log level:

date > now-24h date-asc $group id: level count: $count(1)

How to use

Simple query:

MyQueryString = "house.temperature>23 house.city:\"Milano\" house.pression<:1015 house.when>2017-12-15T10:20:00Z house.when < now house.name-asc",
{ok, Query} = mongoql:parse(MyQueryString),
mongopool_app:find(Pool, Table, Query).

Aggregation query:

MyAggString = "date > now-24h date-asc $group id: level count: $count(1)"
{ok, Agg} = mongoql:agg(MyAggString),
mongopool_app:command(Pool, TableInBinaryString, pipeline, Agg).

Operators

Op. Name Example
< Minor temperature < 10.5
<: Minor Equal temperature <: 7.3
: Equal temperature : 5 or name : "FuuBar"
>: Major Equal temperature >: 2
> Major temperature > 4.4
!: Not Equal temperature !: 4 or name !: "FuuBar"
~ Regex name ~ "Mi*"
in In temperature in [16 17 18] or city in ["Milano" "Roma"]
not Not not temperature > 5 or not name in ["Milano" "Roma"]
exists Exists*(#) name exists or not name exists
{name}-asc Order Ascending name-asc
{name}-desc Order Descending name-desc

(#) matches the documents that contain the field.

Types supported

Type Example
Integer 15, -23, 543
Float 34.56, -235.32
String "Hello world!"
Datetime 2016-01-15T18:19:28Z (Note: without doublequote)
Datetime now (automatically translated with now datetime of the server
Datetime now - 1h, now - 1m, now - 1s

Build

$ ./utils/rebar3 compile

mongoql's People

Contributors

hachreak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.