Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure and query syntax for recursive documents in MongoDB?

I've recently started looking into MongoDB for a project at work. I'm fairly new to JSON and MongoDB's query structure, so I'm hoping one of you can provide some clarification.

I've translated the problem into Excel terminology since it's common and represents my question fairly well.

If I were attempting to model an Excel formula into a MongoDB document, what is the best format to do it in (I'll explain my potential queries lower)? Keep in mind that formulas in Excel can be nested in (nearly) any order, and with any depth, and arguments can come in either string or numerical form. I would like to be able to search across these cells to answer such queries as "Find all cells that use the =AVG() function" or "Find all cells that contain the =SUM() function inside of an =AVG() function (such as =AVG(x,y,z,SUM(a,b,c)))." Being able to answer these formula-structure based queries is more important than being able to answer ones about numbers or strings if answering all isn't possible.

Currently I'm envisioning my documents having roughly the following format:

{
    formula: "AVG",
    arguments: [4,5, {
        formula: "SUM",
        arguments: [6,7,{
            formula: "ABS",
            arguments: [-8,-9]
(closing parenthesis/brackets)
}

Is that a reasonable format for what I'm looking to do? If it is, how would I query for "Find cases with =SUM inside of =AVG"? What about finding the =ABS formula that's nested even deeper? Because of the dynamic nature of formulas, its not really possible to expect a certain order or certain depth.

like image 553
Charles Spinosa Avatar asked Jul 17 '13 19:07

Charles Spinosa


1 Answers

If you have an arbitrary structure like this, then I suggest you store the trees in a different way. Arbitrary structures are difficult to query and deal with.

The MongoDB documentation has a few suggestions: http://docs.mongodb.org/manual/tutorial/model-tree-structures/

like image 63
Derick Avatar answered Sep 28 '22 15:09

Derick