【问题标题】:Querying on nested document in MongoDB查询 MongoDB 中的嵌套文档
【发布时间】:2023-03-08 04:39:01
【问题描述】:

我有一个复杂的文档结构,如下所示 -

{
    "Application": {
        "DEF": {
            "Year": {
                "2018": {
                    "Quarter": {
                        "Q1": {
                            "Microservice": [ "A", "B" ]
                        },
                        "Q2": {
                            "Microservice": [ "C", "D" ]
                        },
                        "Q3": {
                            "Microservice": [ "E" ]
                        },
                        "Q4": {
                            "Microservice": [ "F", "G" ]
                        }
                    }
                },
                "2019": {
                    "Quarter": {
                        "Q1": {
                            "Microservice": [ "A", "C" ]
                        },
                        "Q2": {
                            "Microservice": [ "D" ]
                        },
                        "Q3": {
                            "Microservice": [ "E", "F" ]
                        },
                        "Q4": {
                            "Microservice": [ "G" ]
                        }
                    }
                }
            }
        }
    },
    "Product Name": "XYZ"
}

我正在尝试查询 Application 为 DEF、Year 为 2018 以及所有 Quarters 的所有记录。我已经尝试过如下的 DOT(.) 表示法 --

db.productsTest.find({"Application.DEF.Year.2018": {$exists: true}})

以上返回所有年份(2018 年和 2019 年)的结果,而不是仅返回 2018 年的年份、季度和微服务组合。这也可能是因为 JSON 结构,我无法按年份过滤(因为它们是嵌套的)。基本上我正在寻找返回这个的查询--

{
    "Application": {
        "DEF": {
            "Year": {
                "2018": {
                    "Quarter": {
                        "Q1": {
                            "Microservice": [ "A", "B" ]
                        },
                        "Q2": {
                            "Microservice": [ "C", "D" ]
                        },
                        "Q3": {
                            "Microservice": [ "E" ]
                        },
                        "Q4": {
                            "Microservice": [ "F", "G" ]
                        }
                    }
                }
            }
        }
    },
    "Product Name": "XYZ"
}

考虑到我的 JSON 结构,这个结果是否可能?

【问题讨论】:

  • 您到底想得到什么结果?您能否发布一份您的预期结果文件样本?
  • @dnickless 在您的回答中添加了我的 cmets。

标签: mongodb nested-documents


【解决方案1】:

以下查询可以完成工作:

db.productsTest.find({
    "Application.DEF.Year.2018": { $exists: true } // exclude documents from the result that do not contain the subdocument that we are interested in
}, {
    "_id": 0, // we do not want the _id field in the result document
    "Product Name" : 1, // but the "Product Name" should be included
    "Application.DEF.Year.2018": 1 // and so should be the subdocument we are interested in
})

基本上,这只是标准的queryprojection

$exists 是一个element operator,用于检查属性是否存在。

【讨论】:

  • 我正要回答这个问题,领先我 10-20 秒。
  • 请接受我诚挚的歉意,让我们看看我们的回答是否恰如其分......
  • 我已经稍微改变了 JSON 结构以更好地解释我的问题。基本上我正在寻找上面给出的嵌套文档的查询结构。正如您在 JSON 中看到的,可能有多个年份、季度甚至应用程序。当我知道键的确切值时,您的查询就起作用了——在我的例子中,它是年份(在我们的示例中为 2018 年)和季度(Q1)。但是,如果我只想搜索某个年份的所有季度 - 我该如何实现?感谢您到目前为止的帮助..
  • 好吧..这是一个愚蠢的问题..在这种情况下,查询将更改为 -- db.productsTest.find({"Application.DEF.Year.2018": {$exists:真的}})
  • @Souvik:请给我们一个精确输入文档和预期输出的示例,我们将能够为您提供帮助。
【解决方案2】:

您可以使用$exists 运算符来查找包含指定字段的文档!使用您提供的测试数据,这对我有用:

db.productsTest.findOne({"Application.DEF.Year.2018.Quarter.Q1":{$exists:true}})

并返回您提供的测试文档。

附带说明:除非您有充分的理由深入使用这种嵌套结构,否则扁平化文档有助于提高可读性。

【讨论】:

  • 虽然我同意这种特定文档结构看起来不是最理想的说法,但从性能的角度来看,扁平结构实际上不是首选选项,而深度嵌套是要走的路。 IIRC,这是因为 BSON 处理的工作方式。
  • 射击,在写我的时候提交了类似的答案:/ 谢谢@dnickless,这是我的一个小猜测,我会编辑我的答案
猜你喜欢
  • 2018-03-09
  • 2021-02-25
  • 2016-08-16
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多