Query structure and syntax for recursive documents in MongoDB?

I recently started learning MongoDB for a project at work. I am new to JSON and MongoDB query structure, so I hope one of you can give some clarification.

I translated the problem into Excel terminology, as it is common and presents my question quite well.

If I were trying to simulate an Excel formula in a MongoDB document, what is the best format for this (I will explain my potential queries below)? Keep in mind that formulas in Excel can be embedded (almost) in any order and with any depth, and the arguments can come either in string or in digital form. I would like to be able to search through these cells to answer queries such as “Find all cells that use the = AVG () function” or “Find all cells that contain the = SUM () function inside the = AVG () function ( for example, = AVG (x, y, z, SUM (a, b, c))). The ability to answer these queries based on the formula structure is more important than answering numbers or lines if all this is impossible to answer.

I am currently assuming that my docs are in the following format:

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

Is this a reasonable format for what I'm looking for? If so, how would I request "Find cases with = SUM inside = AVG"? How about finding an ABS formula that fits even deeper? Due to the dynamic nature of the formulas, it is actually impossible to expect a certain order or certain depth.

+6
source share
2 answers

If you have such an arbitrary structure, I suggest you save the trees in a different way. Arbitrary structures are difficult to query and solve.

There are several suggestions in the MongoDB documentation: http://docs.mongodb.org/manual/tutorial/model-tree-structures/

+4
source

Continuing the analogy with Excel:

Firstly, I decided that the request at the second level and beyond is not really needed (this makes the problem much easier). This structure cannot easily adapt to this, therefore a fair warning. This is not a perfect answer to my original question, sorry! It seems that MongoDB is just not very suitable for storing my original question.

I used a structure similar to how MongoDB recommends representing trees. Instead of trying to present an Excel document as one MongoDB document, it is now represented by many (one for each unique formula, and the other for storing strings and values). All MongoDB documents related to the same excel workbook just have a field book and store what they belong to.

A very simple example (custom _id tags _id not needed, but easier to read):

 {_id: book1_1 workbook: book1, cell_values: [1,2,3], cell_strings: ['hello','world']} {_id:book1_2 workbook: book1, formula: 'SUM', children: ['AVG','ABS']} {_id:book1_3 workbook: book1, formula: 'AVG', children: ['SUM']} {_id:book1_4 workbook: book1, formula: 'ABS', children: ['SUM']} 

These 4 MongoDB documents are one Excel document that has the following formula structures (this is not the only Excel worksheet that could create the above MongoDB documents):

 =SUM(AVG()) =AVG(SUM()) =ABS(SUM(ABS())) 

Along with the values ​​1,2,3 and the lines 'hello', 'world' somewhere inside it.

The search query for books with the SUM formula inside the AVG formula is the following query:

 db.collection.find({$and: [{formula: 'AVG'},{children: 'SUM'}]}) 

returns the document _id:book1_3 MongoDB. Then you can disable the workbook, but want to.

0
source

Source: https://habr.com/ru/post/949677/


All Articles