【问题标题】:Mongodb: Query over several collectionsMongodb:查询多个集合
【发布时间】:2019-05-09 01:15:01
【问题描述】:

我有一个 Application 集合,其中包含如下文档:

{
    "_id" : "TAIS",
    "commonUserName" : "TAIS",
    "scopes" : [
        DBRef("Scope", "GEN_ECO"),
        DBRef("Scope", "GEN_PRE")
    ]
}

所以,Scope 集合文件是这样的:

{
    "_id" : "GEN_PRE",
    "code" : "1DPVE000"
}

这里还有第三个集合,Reference:

{
    "_id": "sdfls2",
    "app" : "TAIS"
}

我需要构建一个查询以查找references reference.app belongs to a given %scope% 的位置。

有什么想法吗?

【问题讨论】:

    标签: spring mongodb spring-boot mongodb-query spring-data-mongodb


    【解决方案1】:

    您可以使用$lookup“加入”收藏。 例如

    db.applications.aggregate([
    {
     $lookup:
       {
         from: "third_collection",
         localField: "_id",
         foreignField: "app",
         as: "app_with_reference"
       }
      }
    ])
    

    更多: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#pipe._S_lookup

    【讨论】:

    • 请在您的回答中提供所提供网址的基本要点。
    • 我不太明白,你如何将applications 与scopes 和references 联系起来?
    • 我需要获取其app 属于给定范围的所有引用。你能给我一个小的帮助代码吗?
    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 2011-09-24
    • 2020-07-17
    • 2016-05-02
    • 2016-09-27
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多