草庐IT

mongodb - 将另一个集合中的相关项包含到结果集中

coder 2023-10-27 原文

长见识

如何使用 MongoDB 聚合来包含来自通过一对多关系链接的另一个集合的相关文档?

本质上,我想要做的是能够获取问题列表并包括与该问题关联的所有标志

更新 (11/07/2016):已通过下面发布的解决方案解决。

更新 (05/07/2016): 通过结合使用 $unwind、$lookup、$project<> 等。更新后的查询如下。

问题 (05/07/2016):我只能获取具有嵌套标志的问题。我想提取所有 问题,即使它们没有任何标志。

我有两个集合,一个是content,一个是content flags,如下:

内容架构(问题集)

{
    "_id" : ObjectId("..."),
    "slug" : "a-sample-title",
    "content" : "Some content.",
    "title" : "A Sample Title.",
    "kind" : "Question",
    "updated" : ISODate("2016-06-08T08:54:26.104Z"),
    "isPublished" : true,
    "isFeatured" : false,
    "flags" : [ 
        ObjectId("<id_of_flag_one>"), 
        ObjectId("<id_of_flag_two>")
    ],
    "answers" : [ 
        ObjectId("..."), 
        ObjectId("...")
    ],
    "related" : [],
    "isAnswered" : true,
    "__v" : 4
}

标志的 shcema(标志集合)

{
    "_id" : ObjectId("..."),
    "flaggedBy" : ObjectId("<a_users_id>"),
    "type" : "like",
    "__v" : 0
}

在上面,一个问题可以有多个标志,一个标志只能有一个问题。我想要做的是在查询问题集合时返回问题的所有标志。我曾尝试使用聚合来做到这一点,但运气不错。

这是我正在使用的更新查询 (05/07/2016)

fetchQuestions: (permission, params) => {
    return new Promise((resolve, reject) => {
        let query = Question.aggregate([
            {
                $lookup: {
                    from: 'users',
                    localField: 'author',
                    foreignField: '_id',
                    as: 'authorObject'
                }
            },
            {
                $unwind: '$authorObject'
            },
            {
                $unwind: '$flags'
            },
            {
                $lookup: {
                    from: 'flags',
                    localField: 'flags',
                    foreignField: '_id',
                    as: 'flagObjects'
                }
            },
            {
                $unwind: '$flagObjects'
            },
            {
                $group: {
                    _id: {
                        _id: '$_id',
                        title: '$title',
                        content: '$content',
                        updated: '$updated',
                        isPublished: '$isPublished',
                        isFeatured: '$isFeatured',
                        isAnswered: '$isAnswered',
                        answers: '$answers',
                        author: '$authorObject'
                    },
                    flags: {
                        $push: '$flags'
                    },
                    flagObjects: {
                        $push: '$flagObjects'
                    }
                }
            },
            {
                $project: {
                    _id: 0,
                    _id: '$_id._id',
                    title: '$_id.title',
                    content: '$_id.content',
                    updated: '$_id.updated',
                    isPublished: '$_id.isPublished',
                    isFeatured: '$_id.isFeatured',
                    author: {
                        fullname: '$_id.author.fullname',
                        username: '$_id.author.username'
                    },
                    flagCount: {
                        $size: '$flagObjects'
                    },
                    answersCount: {
                        $size: '$_id.answers'
                    },
                    flags: '$flagObjects',
                    wasFlagged: {
                        $cond: {
                            if: {
                                $gt: [
                                    {
                                        $size: '$flagObjects'
                                    },
                                    0
                                ]
                            },
                            then: true,
                            else: false
                        }
                    }
                }
            },
            {
                $sort: {
                    updated: 1
                }
            },
            {
                $skip: 0
            },
            // {
            //     $limit: 110
            // }
        ])
        .exec((error, result) => {
            if(error) reject(error);
            else resolve(result);
        });
    });
},

我曾尝试使用其他聚合运算符,例如 $unwind$group,但返回的结果集只有五个或更少的项目,我发现很难掌握这些应该如何协同工作以获得我需要的东西的概念。

这是我得到的回应,这正是我所需要的。唯一的问题是,如上所述,我只收到带有标记的问题,而不是所有问题。

"questions": [
{
  "_id": "5757dd42d0c2ae292f76f11a",
  "flags": [
    {
      "_id": "5774e0a81f2874821f71ace8",
      "flaggedBy": "57569d02d0c2ae292f76f0f5",
      "type": "concern",
      "__v": 0
    },
    {
      "_id": "577a0f5414b834372a6ac772",
      "flaggedBy": "5756aa79d0c2ae292f76f0f8",
      "type": "concern",
      "__v": 0
    }
  ],
  "title": "A question for the landing page.",
  "content": "This is a question that will appear on the landing page.",
  "updated": "2016-06-08T08:54:26.104Z",
  "isPublished": true,
  "isFeatured": false,
  "author": {
    "fullname": "Matt Finucane",
    "username": "matfin-386829"
  },
  "flagCount": 2,
  "answersCount": 2,
  "wasFlagged": true
},
...,
...,
...
]

最佳答案

看起来我已经找到了解决这个问题的方法,下面将发布。

我遇到的问题概述如下:

  • 我有一个 Questions 集合,在通常的 ObjectID 字段之上有各种字段,例如标题、内容、发布日期等。

  • 我有一个单独的与问题相关的 Flags 集合。

  • 当为Question 发布Flag 时,该FlagObjectID 应添加到名为flags 附加到 Question 文档。

  • 简而言之,Flags 不直接存储在Question 文档中。对 Flag 的引用存储为 ObjectID

我需要做的是从 Questions 集合中获取所有项目并包含相关标志

MongoDB 聚合框架似乎是解决此问题的理想解决方案,但要了解它可能有点棘手,尤其是在处理 $group$lookup 时> 和 $unwind 运算符。

我还应该指出,我正在使用 NodeJS v6.x.x 和 Mongoose 4.4.x

这是该问题的(相当大的)注释解决方案。

fetchQuestions: (permission, params) => {
    return new Promise((resolve, reject) => {
        let query = Question.aggregate([
            /**
             *  We need to perform a lookup on the author 
             *  so we can include the user details for the 
             *  question. This lookup is quite easy to handle 
             *  because a question should only have one author.
             */
            {
                $lookup: {
                    from: 'users',
                    localField: 'author',
                    foreignField: '_id',
                    as: 'authorObject'
                }
            },
            /**
             *  We need this so that the lookup on the author
             *  object pulls out an author object and not an
             *  array containing one author. This simplifies
             *  the process of $project below.
             */
            {
                $unwind: '$authorObject'
            },
            /**
             *  We need to unwind the flags field, which is an 
             *  array of ObjectIDs. At this stage of the aggregation 
             *  pipeline, questions will be repeated so for example 
             *  if there are two questions and one of them has two 
             *  flags and the other has four flags, the result set 
             *  will have six items and the questions will be repeated
             *  the same number of times as the flags they contain.
             *  The $group function later on will take care of this 
             *  and return only unique questions.
             *
             *  It is important to point out how the $unwind function 
             *  is used here. If we did not specify the preserveNullAndEmptyArrays
             *  parameter then the only questions returned would be those
             *  that have flags. Those without would be skipped.
             */
            {
                $unwind: {
                    path: '$flags',
                    preserveNullAndEmptyArrays: true
                }
            },
            /**
             *  Now that we have the ObjectIDs for the flags from the 
             *  $unwind operation above, we need to perform a lookup on
             *  the flags collection to get our flags. We return these 
             *  with the variable name 'flagObjects' we can use later.
             */
            {
                $lookup: {
                    from: 'flags',
                    localField: 'flags',
                    foreignField: '_id',
                    as: 'flagObjects'
                }
            },
            /**
             *  We then need to perform another unwind on the 'flagObjects' 
             *  and pass them into the next $group function
             */
            {
                $unwind: {
                    path: '$flagObjects',
                    preserveNullAndEmptyArrays: true
                }
            },
            /**
             *  The next stage of the aggregation pipeline takes all 
             *  the duplicated questions with their flags and the flagObjects
             *  and normalises the data. The $group aggregator requires an _id
             *  property to describe how a question should be unique. It also sets
             *  up some variables that can be used when it comes to the $project
             *  stage of the aggregation pipeline.
             *  the flagObjects property calls on the $push function to add a collection
             *  of flagObjects that were pulled from the $lookup above.
             */
            {
                $group: {
                    _id: {
                        _id: '$_id',
                        title: '$title',
                        content: '$content',
                        updated: '$updated',
                        isPublished: '$isPublished',
                        isFeatured: '$isFeatured',
                        isAnswered: '$isAnswered',
                        answers: '$answers',
                        author: '$authorObject'
                    },
                    flagObjects: {
                        $push: '$flagObjects'
                    }
                }
            },
            /**
             *  The $project stage of the pipeline then puts together what the final 
             *  result set should look like when the query is executed. Here we can use
             *  various Mongo functions to reshape the data and create new attributes.
             */
            {
                $project: {
                    _id: 0,
                    _id: '$_id._id',
                    title: '$_id.title',
                    updated: '$_id.updated',
                    isPublished: '$_id.isPublished',
                    isFeatured: '$_id.isFeatured',
                    author: {
                        fullname: '$_id.author.fullname',
                        username: '$_id.author.username'
                    },
                    flagCount: {
                        $size: '$flagObjects'
                    },
                    answersCount: {
                        $size: '$_id.answers'
                    },
                    flags: '$flagObjects',
                    wasFlagged: {
                        $cond: {
                            if: {
                                $gt: [
                                    {
                                        $size: '$flagObjects'
                                    },
                                    0
                                ]
                            },
                            then: true,
                            else: false
                        }
                    }
                }
            },
            /**
             *  Then we can sort, skip and limit if needs be.
             */
            {
                $sort: {
                    updated: -1
                }
            },
            {
                $skip: 0
            },
            // {
            //     $limit: 110
            // }
        ]);

        query.exec((error, result) => {
            if(error) reject(error);
            else resolve(result);
        });
    });
},

这是返回的示例

"questions": [
    {
      "_id": "576a85d68c4333a017083fca",
      "title": "How do I do this?",
      "updated": "2016-06-22T12:34:30.919Z",
      "isPublished": false,
      "isFeatured": false,
      "author": {
        "fullname": "Matt Finucane",
        "username": "matfin-386829"
      },
      "flagCount": 1,
      "answersCount": 0,
      "flags": [
        {
          "_id": "5776541a2e38844428696615",
          "flaggedBy": "5756aa79d0c2ae292f76f0f8",
          "type": "concern",
          "__v": 0
        }
      ],
      "wasFlagged": true
    },
    {
      "_id": "576a85d68c4333a017083fc9",
      "title": "Is this a question?",
      "updated": "2016-06-22T12:34:30.918Z",
      "isPublished": true,
      "isFeatured": false,
      "author": {
        "fullname": "Matt Finucane",
        "username": "matfin-386829"
      },
      "flagCount": 2,
      "answersCount": 0,
      "flags": [
        {
          "_id": "5773ce4ea363e5161ae69e7f",
          "flaggedBy": "5756aa79d0c2ae292f76f0f8",
          "type": "concern",
          "__v": 0
        },
        {
          "_id": "577654382e3884442869661d",
          "flaggedBy": "57569d02d0c2ae292f76f0f5",
          "type": "concern",
          "__v": 0
        }
      ],
      "wasFlagged": true
    }
]

关于mongodb - 将另一个集合中的相关项包含到结果集中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38132489/

有关mongodb - 将另一个集合中的相关项包含到结果集中的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  8. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  9. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  10. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

随机推荐