MongoDB 中递归文档的结构和查询语法? [英] Structure and query syntax for recursive documents in MongoDB?

查看:49
本文介绍了MongoDB 中递归文档的结构和查询语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始研究 MongoDB 的工作项目.我对 JSON 和 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.

我已将问题翻译成 Excel 术语,因为它很常见并且很好地代表了我的问题.

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

如果我试图将 Excel 公式建模到 MongoDB 文档中,那么最好的格式是什么(我将在下面解释我的潜在查询)?请记住,Excel 中的公式可以(几乎)以任何顺序和任何深度嵌套,并且参数可以以字符串或数字形式出现.我希望能够在这些单元格中进行搜索以回答诸如查找所有使用 =AVG() 函数的单元格"或在 =AVG() 函数中查找包含 =SUM() 函数的所有单元格"之类的查询(例如 =AVG(x,y,z,SUM(a,b,c)))."如果无法回答所有问题,能够回答这些基于公式结构的查询比能够回答有关数字或字符串的问题更重要.

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)
}

对于我想要做的事情,这是一种合理的格式吗?如果是,我将如何查询在 =AVG 中查找 =SUM 的案例"?如何找到嵌套更深的 =ABS 公式?由于公式的动态性,实际上不可能期望一定的顺序或一定的深度.

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.

推荐答案

如果您有这样的任意结构,那么我建议您以不同的方式存储树.任意结构难以查询和处理.

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.

MongoDB 文档有一些建议:http://docs.mongodb.org/manual/tutorial/model-tree-structures/

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

这篇关于MongoDB 中递归文档的结构和查询语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆