jjzjj

node.js - MongoDB 聚合管道查询

coder 2023-11-05 原文

我在 node.js 8.11.1 上使用 mongoDB 3.6,并使用 MongoDB Node.js 驱动程序。 我有两个集合,“组”和“用户”:

group:
[  
   {  
      "_id":1,
      "groupName":"group1",
      "users":[  
         {  
            "userId":1,
            "isAdmin":"false"
         },
         {  
            "userId":2,
            "isAdmin":"true"
         }
      ]
   },
   {  
      "_id":2,
      "groupName":"group2",
      "users":[  
         {  
            "userId":2,
            "isAdmin":"false"
         },
         {  
            "userId":3,
            "isAdmin":"true"
         }
      ]
   }
]

user:
[  
   {  
      "_id":1,
      "username":"user1",
      "firstname":"a",
      "lastname":"aa",
      "mobileNo":"+1111111"
   },
   {  
      "_id":2,
      "username":"user2",
      "firstname":"b",
      "lastname":"bb",
      "mobileNo":"+2222222"
   },
   {  
      "_id":3,
      "username":"user3",
      "firstname":"c",
      "lastname":"cc",
      "mobileNo":"+3333333"
   }
]

我需要一个聚合来返回这样的东西:

[  
   {  
      "_id":1,
      "groupName":"group1",
      "members":[  
         {  
            "isAdmin":"false",
            "username":"user1",
            "firstname":"a",
            "lastname":"aa"
         },
         {  
            "isAdmin":"true",
            "username":"user2",
            "firstname":"b",
            "lastname":"bb"
         }
      ]
   },
   {  
      "_id":2,
      "groupName":"group2",
      "members":[  
         {  
            "isAdmin":"false",
            "username":"user2",
            "firstname":"b",
            "lastname":"bb"
         },
         {  
            "isAdmin":"true",
            "username":"user3",
            "firstname":"c",
            "lastname":"cc"
         }
      ]
   }
]

在结果的“members”中,“isAdmin”从组集合中的“users”返回,“username”、“firstname”和“lastname”来自用户集合

非常感谢,

米拉德。

最佳答案

您可以尝试从 mongodb 3.6 及更高版本进行以下聚合

db.group.aggregate([
  { "$unwind": "$users" },
  { "$lookup": {
    "from": Users.collection.name,
    "let": { "userId": "$users.userId", "isAdmin": "$users.isAdmin" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$userId" ] } } },
      { "$project": { "isAdmin": "$$isAdmin", "username": 1, "firstName": 1, "lastName": 1 }}
    ],
    "as": "members"
  }},
  { "$unwind": "$members" },
  { "$group": {
    "_id": "$_id",
    "members": { "$push": "$members" },
    "groupName": { "$first": "$groupName" }
  }}
])

关于node.js - MongoDB 聚合管道查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52363378/

有关node.js - MongoDB 聚合管道查询的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  3. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  4. ruby-on-rails - solr 清理查询 - 2

    我在Rails上使用带有ruby​​的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s

  5. ruby-on-rails - Rails 3 在一个查询中包含多个表 - 2

    我正在为锦标赛开发一个Rails应用程序。我在这个查询中使用了三个模型:classPlayertruehas_and_belongs_to_many:tournamentsclassTournament:destroyclassPlayerMatch"Player",:foreign_key=>"player_one"belongs_to:player_two,:class_name=>"Player",:foreign_key=>"player_two"在tournaments_controller的显示操作中,我调用以下查询:Tournament.where(:id=>params

  6. ruby-on-rails - Sunspot:如何对具有不同值的多个字段进行全文查询? - 2

    我想用sunspot重现以下原始solr查询q=exact_term_text:fooORterm_textv:foo*ORalternate_text:bar*但我无法通过标准的太阳黑子界面理解这是否可能以及如何实现,因为看起来:fulltext方法似乎不接受多个文本/搜索字段参数我不知道将什么参数作为第一个参数传递给fulltext,就好像我通过了"foo"或"bar"结果不匹配如果我传递一个空参数,我得到一个q=*:*范围过滤器(例如with(:term).starting_with('foo*')(顾名思义)作为过滤器查询应用,因此不参与评分。似乎可以手动编写字符串(或者可能使

  7. ruby-on-rails - 在不重新查询数据库的情况下重新排序 Rails 中的事件记录? - 2

    例如,假设我有一个名为Products的模型,并且在ProductsController中,我有以下代码用于product_listView以显示已排序的产品。@products=Product.order(params[:order_by])让我们想象一下,在product_listView中,用户可以使用下拉菜单按价格、评级、重量等进行排序。数据库中的产品不会经常更改。我很难理解的是,每次用户选择新的order_by过滤器时,rails是否必须查询,或者rails是否能够以某种方式缓存事件记录以在服务器端重新排序?有没有一种方法可以编写它,以便在用户排序时rails不会重新查询结果

  8. ruby-on-rails - 带句点(或句号)的 Rails 查询字符串。 - 2

    我目前正在尝试了解RoR。我将两个字符串传递到我的Controller中。一个是随机的十六进制字符串,另一个是电子邮件。该项目用于对数据库进行简单的电子邮件验证。我遇到的问题是当我输入如下内容来测试我的页面时:http://signup.testsite.local/confirm/da2fdbb49cf32c6848b0aba0f80fb78c/bob.villa@gmailcom我在:email的参数散列中得到的全部是'bob'。我在gmail和com之间留下了.,因为那样会导致匹配根本不起作用。我的路由匹配如下:match"confirm/:code/:email"=>"conf

  9. ruby-on-rails - Assets 管道损坏 : Not compiling on the fly css and js files - 2

    我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1

  10. ruby-on-rails - Rails - 理解 application.js 和 application.css - 2

    rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:

随机推荐