Array,Object Operators

MongoDB provides many aggregation operators and MQL is so powerful.
But still some common used operations are hard to use in MQL.

MQL provides stage operators but those are made for collections(not arrays), and for root documents(not nested documents)

Missing array operator

Add 1 member to the end fast ($concat can do it but slow)

Common use case is when reducing one array to another array.
The bellow is usable only if :myarray size <500 members.

(reduce (fn [:new-array. :mem.]
(conj :new-array :mem.))
[]
:myarray)

Effects only big arrays(100+ members),and multiple calls of concat
If we dont have big arrays the above works without problems

Missing document operators

MQL doesn't provide operators like the bellow

  • contains(k)
  • get(k)
  • assoc(k,v) (assoc => add new pair)
  • dissoc(k) (dissoc => remove key)

k,v could be

  • literal
  • field reference
  • variable

Common use case is when we group object of array based on some property. See Groups

Missing nested Document operators

Many times we need to use those,nested.

For example ,get into an array,then a document,then an array etc ...

(get-in :myarray [1 "b" 2])

Solutions

Stage operators

Stage operators like the ones that we use to $addFields,$project,$group etc
They work for collections,not for arrays or nested documents

We can't just use a stage operator inside an expression
They also don't allow us to use expressions on them,like $project "$$myvar"

We can unwind arrays,or merge documents with the root,but those operations

  • they change the document structure
  • we need to re-structure the document
  • even if we can do it,code can be complicated
  • and it can be slower also

Keeping data inside arrays, is simpler and fits more with the document based MongoDB model,and with sharded collections.

Javascript

We can use Javascript with $function,and $accumulator and do all the above easily.

But Javascript is slower than aggragation operators,and its a different language.

cMQL

cMQL uses MQL operators,that reduces the code complexity,and make it easier to process arrays and documents in place,without changing the document structure.

For cMQL solutions see