<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Formalizing MongoDB Queries</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Elena Botoeva</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Diego Calvanese</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Benjamin Cogrel</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Guohui Xiao</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Free University of Bozen-Bolzano</institution>
          ,
          <country country="IT">Italy</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>In this paper, we report on our ongoing work in which we formalize MongoDB, a widely adopted document database system managing complex (tree structured) values represented in a JSON-based data model, equipped with a powerful query mechanism. We study the expressiveness of the MongoDB query language, showing its equivalence with nested relational algebra, and we investigate the computational complexity of significant fragments of it.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1 Introduction</title>
      <p>
        In the past decade numerous database (DB) architectures and data models have been
proposed as attempts to better address the varying demands of modern data-intensive
applications. Many of these new systems do not rely on the relational model but instead
adopt a semi-structured data format, and alternative query mechanisms, which combine
an increased flexibility in data handling, with a higher efficiency (at least for the most
common operations). These systems are generally categorized under the term NoSQL
(which stands for “not only SQL”) [
        <xref ref-type="bibr" rid="ref13 ref6">6,13</xref>
        ].
      </p>
      <p>
        A popular design among these non-relational systems consists in organizing data in
collections of semi-structured, tree-shaped documents in the JavaScript Object Notation
(JSON) format. Such documents can be seen as complex values [
        <xref ref-type="bibr" rid="ref1 ref18 ref8 ref9">9,1,18,8</xref>
        ], in particular
when they contain nested arrays. As an example, consider the document in Figure 1,
containing personal information about Kristen Nygaard (e.g., name and birth-date)
together with information about the awards he received, stored in an array.
      </p>
      <p>
        Unsurprisingly, many similarities can be observed between non-relational
languages for querying JSON collections having rich capabilities (see, e.g., [
        <xref ref-type="bibr" rid="ref14 ref16 ref2">2,14,16</xref>
        ]),
and well-known query languages for complex values, such as nested relational algebra
(NRA) [
        <xref ref-type="bibr" rid="ref15 ref17">15,17</xref>
        ], monad algebra [
        <xref ref-type="bibr" rid="ref12 ref5">5,12</xref>
        ] and Core XQuery [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ]. However, the formal
semantics and the computational properties of these query languages are still largely not
understood and are being actively investigated [
        <xref ref-type="bibr" rid="ref10 ref4">4,10</xref>
        ].
      </p>
      <p>In this work, we conduct the first major study into the formal foundations and
properties of the data model and query language of MongoDB, a widely adopted
distributed JSON-based document database. MongoDB provides rich querying capabilities
by means of the aggregation framework1. In this framework, a query is a multi-stage
pipeline, where each stage defines a transformation, using a MongoDB-specific
operator, applied to the set of documents produced by the previous stage.</p>
      <p>Our first contribution is a formalization of the MongoDB data model and of the
fragment of the aggregation framework query language that includes the match, unwind,
project, group, and lookup operators, and which we call MQuery. Each of these
operators roughly corresponds to an operator of NRA: match corresponds to select, project
to project, lookup to left join, group to nest, and unwind to unnest.</p>
      <p>Our second contribution is a characterization of the expressive power of MQuery
obtained by comparing it with NRA. We devise translations in both directions between
the two languages showing that they are equivalent in expressive power.</p>
      <p>Finally, we carry out an investigation of the computational complexity of MMUPGL
and of various fragments of it. Interestingly, since our translations between MQuery
and NRA are compact (i.e., polynomial), they allow us also to carry over complexity
results between MQuery and NRA.</p>
      <p>
        We provide here only an overview, and refer to the full version for more details [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ].
2
      </p>
    </sec>
    <sec id="sec-2">
      <title>MQuery</title>
      <p>Objects in the JSON format are defined inductively as consisting of key-value pairs,
where a key is a string, and a value can be a literal, an object, or an array of values,
constructed inductively according to the grammar in Figure 2 (where terminals are written
1 https://docs.mongodb.com/core/aggregation-pipeline/</p>
      <p>id
4 []</p>
      <p>0 1
ffgg ffgg
award by year award by year
RPorsiizneg ANsosrDowcaeitagatiiaonn 1999 TAuwrainrdg ACM 2001
awards
2</p>
      <p>ffgg
award by year
voInEENEeuJmohannn IEEE 2001</p>
      <p>Medal
ffgg
birth
1926-08-27
contribs death
[]
name</p>
      <p>2002-08-10
0 1
OOP Simula</p>
      <p>ffgg
first last
Kristen Nygaard</p>
      <p>Fig. 3. The tree representation of the MongoDB document in Figure 1
' ::= p = v j 9p j ' _ ' j ' ^ ' j :'
d ::= v j p j [d; : : : ; d] j j
::= p = p j p = v j v = v j 9p j d _ d j d ^ d j :d
::= (d?d:d)</p>
      <p>P ::= p j p=d j p; P j p=d; P
G; A ::= p=p0 j p=p0; G</p>
      <p>s ::= ' j !pn j nPi j G:A j pp1=C:p2
MQuery ::= C . s . . s
in black, and non-terminals in blue). The set of key-value pairs constituting a JSON
object may not contain the same key twice. A MongoDB database stores collections of
documents, where each collection has a name, and consists of a finite set of documents.
Each document is a JSON object (not nested within any other object) with a special key
‘ id’, which is used to identify the document. Figure 1 shows a MongoDB document
in which, apart from id, the keys are birth, name, awards, etc. Intuitively, a
collection corresponds to a table in a (nested) relational database, and a document to a row
in a table. We formalize documents as finite unordered, unranked, node-labeled, and
edge-labeled trees, and collections as forests. The tree corresponding to the document
in Figure 1 is depicted in Figure 3.</p>
      <p>MongoDB is equipped with a powerful query mechanism provided by the
aggregation framework. As a first contribution, we formalize the core aspects of such query
language. We call our language MQuery, and consider also different fragments of it.
An MQuery is a sequence of stages s, also called a pipeline, applied to a collection
name C, where each of the stages roughly corresponds to a relational algebra operation,
and transforms a forest into another forest. Here we are not concerned with syntactic
aspects of MQuery, and instead propose for it an algebra, shown in Figure 4.</p>
      <p>In an MQuery, paths, which are (possibly empty) concatenations of keys, are used
to access actual values in a tree, similarly to how attributes are used in relational algebra.
We use " to denote the empty path. MQuery allows for five types of stages:
– match ', selecting trees according to the criterion ', which is a Boolean
combination of atomic conditions that express either the equality of a path p to a value v, or
the existence of a path p.
– unwind !p and !p+, which flatten an array reached through a path p in the input tree,
and output a tree for each element of the array; the latter operator preserves a tree
even when the array does not exist or is empty.
– project P and Pid , which modify trees by projecting away paths, renaming paths, or
introducing new paths; the latter version projects away id, which otherwise is kept
by default. Here P is a sequence of elements of the form p or q=d, where p is a path
to be kept, and q is a new path whose value is defined by d. Such a value definition
d can provide for q a constant v, the value reached through a path p (i.e., renaming
path p to q), a new array defined through its values, the value of a Boolean expression
, or a value computed through a conditional expression . Note that, in a Boolean
value definition , one can also compare the values of two paths, while in a criterion
' one can only compare the value of a path to a constant value. Also note that each
value definition can be evaluated to a Boolean value.
– group G:A, which groups trees according to a grouping condition G and collects
values of interest according to an aggregation condition A. Both G and A are
(possibly empty) sequences of elements of the form p=p0, where p0 is a path in the input
documents, and p a path in the output document. In these sequences, if p coincides
with p0, then we simply write p instead of p=p. Each group in the output will have an
id whose value is given by the values of p0 in G for that group.
– lookup pp1=C:p2 , which joins input trees with trees in an external collection C, using
a local path p1 and a path p2 in C to express the join condition, and uses a path p to
store the matching trees in an array.</p>
      <p>We consider also various fragments of MQuery, and we denote each fragment by
M , where consists of the initials of the stages that can be used in queries in the
fragment. Hence, MMUPGL denotes MQuery itself, e.g., MMUPG the fragment of MMUPGL
that does not use lookup, and MMUP the fragment that additionally does not use group.
3</p>
    </sec>
    <sec id="sec-3">
      <title>Results</title>
      <p>We study the expressiveness and the computational complexity of MQuery.</p>
      <p>First, we show that for MongoDB instances of a certain regular structure, MQuery
is essentially equivalent to nested relational algebra (NRA). Regularity of MongoDB
instances allows for defining a nested relational view of MongoDB documents, which
serves for establishing the correctness of the translations between MQuery and NRA,
and hence the equivalence between MMUPGL and NRA. We also consider the MMUPG
fragment, where we rule out the lookup operator, which allows for joining a given
document collection with external ones, and establish that already MMUPG is equivalent to
NRA over a single relation, and hence is capable of expressing arbitrary joins (within
one collection), contrary to what is believed in the community of MongoDB
practitioners and users.</p>
      <p>
        Second, we obtain the exact complexity of MMUPGL and of some of its fragments:
– What we consider the minimal fragment, namely MM, which allows only for match,
is LOGSPACE-complete in combined complexity.
– Projection and grouping allow one to create exponentially large objects, but by
representing intermediate results compactly as DAGs, one can still evaluate MMPGL
queries in PTIME. Specifically, MMP is PTIME-hard in query complexity and MMPGL
is in PTIME in combined complexity.
– For MMU, the use of unwind causes loss of tractability in combined complexity,
specifically it leads to NP-completeness, but the language remains
LOGSPACEcomplete in query complexity.
– Further adding project in MMUP, or lookup in MMUL, leads again to NP-harness even
in query complexity, although MMUPL stays NP-complete in combined complexity.
– In the presence of unwind, grouping provides another source of complexity, since it
allows one to create doubly-exponentially large objects; indeed MMUG is
PSPACEhard in query complexity.
– The full language MMUPGL and also the MMUPG fragment are complete for
TA[2nO(1); nO(1)] (i.e., exponential time with a polynomial number of
alternations [
        <xref ref-type="bibr" rid="ref11 ref7">7,11</xref>
        ]) in combined complexity, and in AC0 in data complexity.
The latter result provides also a tight TA[2nO(1); nO(1)] bound for the combined
complexity of Boolean query evaluation in NRA, whose exact complexity was open [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ].
Acknowledgements. This research has been partially supported by the project
“Ontology-based Data Access for NoSQL Databases” (OBDAM), funded through the
2016 call issued by the Research Committee of the Free University of Bozen-Bolzano.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>S.</given-names>
            <surname>Abiteboul</surname>
          </string-name>
          and
          <string-name>
            <given-names>C.</given-names>
            <surname>Beeri</surname>
          </string-name>
          .
          <article-title>The power of languages for the manipulation of complex values</article-title>
          .
          <source>Very Large Database J.</source>
          ,
          <volume>4</volume>
          (
          <issue>4</issue>
          ):
          <fpage>727</fpage>
          -
          <lpage>794</lpage>
          ,
          <year>1995</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>K. S.</given-names>
            <surname>Beyer</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            <surname>Ercegovac</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Gemulla</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Balmin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Eltabakh</surname>
          </string-name>
          ,
          <string-name>
            <surname>C.-C. Kanne</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          <string-name>
            <surname>Ozcan</surname>
            , and
            <given-names>E. J.</given-names>
          </string-name>
          <string-name>
            <surname>Shekita</surname>
          </string-name>
          .
          <article-title>Jaql: A scripting language for large scale semistructured data analysis</article-title>
          .
          <source>Proc. of the VLDB Endowment</source>
          ,
          <volume>4</volume>
          (
          <issue>12</issue>
          ):
          <fpage>1272</fpage>
          -
          <lpage>1283</lpage>
          ,
          <year>2011</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>E.</given-names>
            <surname>Botoeva</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Calvanese</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Cogrel</surname>
          </string-name>
          , and
          <string-name>
            <given-names>G.</given-names>
            <surname>Xiao</surname>
          </string-name>
          .
          <article-title>Expressivity and complexity of MongoDB (Extended Version)</article-title>
          .
          <source>CoRR Technical Report arXiv:1603.09291</source>
          , arXiv.org e-Print archive,
          <year>2017</year>
          . Available at http://arxiv.org/abs/1603.09291.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>P.</given-names>
            <surname>Bourhis</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. L.</given-names>
            <surname>Reutter</surname>
          </string-name>
          ,
          <string-name>
            <surname>F.</surname>
          </string-name>
          <article-title>Sua´rez, and D. Vrgocˇ</article-title>
          . JSON:
          <article-title>Data model, query languages and schema specification</article-title>
          .
          <source>In Proc. of the 36th Symp. on Principles of Database Systems (PODS)</source>
          , pages
          <fpage>123</fpage>
          -
          <lpage>135</lpage>
          ,
          <year>2017</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>P.</given-names>
            <surname>Buneman</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Naqvi</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            <surname>Tannen</surname>
          </string-name>
          , and
          <string-name>
            <given-names>L.</given-names>
            <surname>Wong</surname>
          </string-name>
          .
          <article-title>Principles of programming with complex objects and collection types</article-title>
          .
          <source>Theoretical Computer Science</source>
          ,
          <volume>149</volume>
          (
          <issue>1</issue>
          ):
          <fpage>3</fpage>
          -
          <lpage>48</lpage>
          ,
          <year>1995</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>R.</given-names>
            <surname>Cattell</surname>
          </string-name>
          .
          <article-title>Scalable SQL and NoSQL data stores</article-title>
          .
          <source>SIGMOD Record</source>
          ,
          <volume>39</volume>
          (
          <issue>4</issue>
          ):
          <fpage>12</fpage>
          -
          <lpage>27</lpage>
          , May
          <year>2011</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>A. K. Chandra</surname>
            ,
            <given-names>D. C.</given-names>
          </string-name>
          <string-name>
            <surname>Kozen</surname>
            , and
            <given-names>L. J.</given-names>
          </string-name>
          <string-name>
            <surname>Stockmeyer</surname>
          </string-name>
          . Alternation.
          <source>Journal of the ACM</source>
          ,
          <volume>28</volume>
          (
          <issue>1</issue>
          ):
          <fpage>114</fpage>
          -
          <lpage>133</lpage>
          ,
          <year>1981</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>E.</given-names>
            <surname>Dantsin</surname>
          </string-name>
          and
          <string-name>
            <given-names>A.</given-names>
            <surname>Voronkov</surname>
          </string-name>
          .
          <article-title>Complexity of query answering in logic databases with complex values</article-title>
          .
          <source>In Proc. of the 4th Int. Symp. on Logical Foundations of Computer Science (LFCS)</source>
          , pages
          <fpage>56</fpage>
          -
          <lpage>66</lpage>
          ,
          <year>1997</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <given-names>S.</given-names>
            <surname>Grumbach</surname>
          </string-name>
          and
          <string-name>
            <given-names>V.</given-names>
            <surname>Vianu</surname>
          </string-name>
          .
          <article-title>Tractable query languages for complex object databases</article-title>
          .
          <source>In Proc. of the 10th Symp. on Principles of Database Systems (PODS)</source>
          ,
          <year>1991</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>J. Hidders</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          <string-name>
            <surname>Paredaens</surname>
          </string-name>
          , and J.
          <string-name>
            <surname>Van den Bussche.</surname>
          </string-name>
          J-Logic:
          <article-title>Logical foundations for JSON querying</article-title>
          .
          <source>In Proc. of the 36th Symp. on Principles of Database Systems (PODS)</source>
          , pages
          <fpage>137</fpage>
          -
          <lpage>149</lpage>
          ,
          <year>2017</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <given-names>D. S.</given-names>
            <surname>Johnson</surname>
          </string-name>
          .
          <article-title>A catalog of complexity classes</article-title>
          .
          <source>In Handbook of Theoretical Computer Science</source>
          , volume
          <string-name>
            <surname>A</surname>
          </string-name>
          , chapter
          <volume>2</volume>
          . Elsevier Science Publishers,
          <year>1990</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          12.
          <string-name>
            <given-names>C.</given-names>
            <surname>Koch</surname>
          </string-name>
          .
          <article-title>On the complexity of nonrecursive XQuery and functional query languages on complex values</article-title>
          .
          <source>ACM Trans. on Database Systems</source>
          ,
          <volume>31</volume>
          (
          <issue>4</issue>
          ):
          <fpage>1215</fpage>
          -
          <lpage>1256</lpage>
          ,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          13.
          <string-name>
            <given-names>N.</given-names>
            <surname>Leavitt</surname>
          </string-name>
          .
          <article-title>Will NoSQL databases live up to their promise</article-title>
          ?
          <source>Computer</source>
          ,
          <volume>43</volume>
          (
          <issue>2</issue>
          ):
          <fpage>12</fpage>
          -
          <lpage>14</lpage>
          , Feb.
          <year>2010</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          14.
          <string-name>
            <surname>C. Olston</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          <string-name>
            <surname>Reed</surname>
            , U. Srivastava,
            <given-names>R.</given-names>
          </string-name>
          <string-name>
            <surname>Kumar</surname>
          </string-name>
          ,
          <article-title>and</article-title>
          <string-name>
            <given-names>A.</given-names>
            <surname>Tomkins</surname>
          </string-name>
          .
          <article-title>Pig Latin: a not-so-foreign language for data processing</article-title>
          .
          <source>In Proc. of the ACM SIGMOD Int. Conf. on Management of Data</source>
          , pages
          <fpage>1099</fpage>
          -
          <lpage>1110</lpage>
          ,
          <year>2008</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          15. S. J. Thomas and
          <string-name>
            <given-names>P. C.</given-names>
            <surname>Fischer</surname>
          </string-name>
          .
          <article-title>Nested relational structures</article-title>
          .
          <source>Advances in Computing Research</source>
          ,
          <volume>3</volume>
          :
          <fpage>269</fpage>
          -
          <lpage>307</lpage>
          ,
          <year>1986</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          16.
          <string-name>
            <given-names>A.</given-names>
            <surname>Thusoo</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. S.</given-names>
            <surname>Sarma</surname>
          </string-name>
          ,
          <string-name>
            <given-names>N.</given-names>
            <surname>Jain</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Z.</given-names>
            <surname>Shao</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P.</given-names>
            <surname>Chakka</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Anthony</surname>
          </string-name>
          , H. Liu,
          <string-name>
            <given-names>P.</given-names>
            <surname>Wyckoff</surname>
          </string-name>
          , and
          <string-name>
            <given-names>R.</given-names>
            <surname>Murthy</surname>
          </string-name>
          .
          <article-title>Hive: A warehousing solution over a map-reduce framework</article-title>
          .
          <source>Proc. of the VLDB Endowment</source>
          ,
          <volume>2</volume>
          (
          <issue>2</issue>
          ):
          <fpage>1626</fpage>
          -
          <lpage>1629</lpage>
          ,
          <year>2009</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          17. J. Van den Bussche.
          <article-title>Simulation of the nested relational algebra by the flat relational algebra, with an application to the complexity of evaluating powerset algebra expressions</article-title>
          .
          <source>Theoretical Computer Science</source>
          ,
          <volume>254</volume>
          (
          <issue>1</issue>
          ):
          <fpage>363</fpage>
          -
          <lpage>377</lpage>
          ,
          <year>2001</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref18">
        <mixed-citation>
          18. J. Van den Bussche and
          <string-name>
            <surname>J. Paredaens.</surname>
          </string-name>
          <article-title>The expressive power of complex values in objectbased data models</article-title>
          .
          <source>Information and Computation</source>
          ,
          <volume>120</volume>
          (
          <issue>2</issue>
          ):
          <fpage>220</fpage>
          -
          <lpage>236</lpage>
          ,
          <year>1995</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>