Operators overview

cMQL supports all aggregation operators see the API
Bellow are some more complicated ones.

For an overview see Example2

if cond

(q :testdb.testcoll
:f (if- (= :a 1) 2 3)
:c (cond (= :a 1) 2
(= :a 2) 5
:else 10)})


let allows depended variables also, producing the minimun possible nested $let. (for now don't define the same variable multiple times in the same let)

(let [:a. 1
:b. 2
:c. (+ :a. :b.)
:d. (+ :a. :c.)
:e. (+ :b. :a. :d.)
:k. (+ :b. :a. :d. :c. :e.)
:z. 3
:l. :c.]

map filter reduce

(filter (fn [:m.] (some? :m.)) :myarray)
(map (fn [:i.] (+ :i. 1)) :myarray)
(reduce (fn [:sum. :e.] (+ :sum. :e.)) 0 :myarray)

*if i don't want names for the arguments i can use :value. or :this. like

(filter (fn [:this.] (some? :this.)) :myarray)
(map (fn [:this.] (+ :this. 1)) :myarray)
(reduce (fn [:value. :this.] (+ :value. :this.)) 0 :myarray)

functions defmfn defnmfn

MQL doens't have functions
But we can use the driver and cMQL like functions

driver functions

If we want to use a function to generate cMQL we can do it,using the macro cmql like

(defn myfn [n]
(cmql (+ n 2))) ;;when called will generate {"$add" [n 2]}

This can be used for code re-use or for very big queries

mongo functions

MQL doens't have functions for map/filter/reduce it uses a variable key "vars",and a body "in"
cMQL makes them look like normal functions

(map (fn [:i.] (+ :i. 1)) :myarray)

But we can go even futher and def them as independent functions

defnmfn Allows normal clojure code to be used, we call them before we use them see bellow

(defnmfn myf [m]
(str "hello " m)) ;;body can work both for clojure and cMQL
(q :testdb.testcoll
{:a (map (myf :m.) :myarray)}))

defmfn We declare them as mongo-functions,they use mongo-variables inside the body,and they dont take arguments when are called.

(defnmfn myf1 [:m.]
(str "hello " :m.))
(q :testdb.testcoll
{:a (map myf :myarray)})

All those functions can only be use cMQL operators(or raw MQL) in their body,but because cMQL is like Clojure we can have clojure code re-use.
If we want to use clojure inside them we can use c/str (c alias for clojure.core)