Mongodb 带条件聚合查找

2023-12-02

我有一个名为article_category其中存储所有article_id属于以下类别category_id像这样的数据格式。

集合 1:article_category

{
  "article_id": 2015110920343902,
  "all_category_id": [5,8,10]
}

然后我还有其他集合称为article其中存储了我所有的帖子

收藏2:文章

{
  "title": "This is example rows in article collection"
  "article_id": 2015110920343902,
},
{
  "title": "Something change"
  "article_id": 2015110920343903,
},
{
  "title": "This is another rows",
  "article_id": 2015110920343904,
}

现在我想执行 MongoDB 查询来查找title with regex while category_id必须等于8。这是我的查询,但不起作用。

db.article.aggregate(
{
  $match: 
  {
    title: 
    {
       $regex: /example/
    }
  }
},
{
    $lookup:
       {
         from: "article_category",
         pipeline: [
            { $match: { category_id: 8 } }
         ],
         as: "article_category"
       }
  }
)

以上查询仅显示符合以下条件的记录regex但不匹配category_id.

任何想法?


首先,它是all_category_id, not category_id。其次,您不链接文章 - 所有文档都将具有完全相同的内容article_category大批。最后,您可能想过滤掉没有匹配类别的文章。条件管道应该看起来更像这样:

db.article.aggregate([
  { $match: {
      title: { $regex: /example/ }
  } },
  { $lookup: {
    from: "article_category",
    let: {
      article_id: "$article_id"
    },
    pipeline: [
      { $match: {
          $expr: { $and: [
              { $in: [ 8, "$all_category_id" ] },
              { $eq: [ "$article_id", "$$article_id" ] }
          ] }
      } }
    ],
    as: "article_category"
  } },
  { $match: {
    $expr: { $gt: [
      { $size: "$article_category"},
      0
    ] }
  } }
] )

UPDATE:

如果你不匹配article_id, the $lookup将得到相同的结果article_category数组到所有文章。

比方说你的article_category集合还有另一个文档:

{
  "article_id": 0,
  "all_category_id": [5,8,10]
}

With { $eq: [ "$article_id", "$$article_id" ] }在管道中产生的article_category is

[ 
  { 
    "article_id" : 2015110920343902, 
    "all_category_id" : [ 5, 8, 10 ] 
  } 
]

without:

[ 
  { 
    "article_id" : 2015110920343902, 
    "all_category_id" : [ 5, 8, 10 ] 
  },
  {
    "article_id": 0,
    "all_category_id": [ 5, 8, 10 ]
  }
]

如果您需要后者,则查找请求会更简单:

db.article.find({ title: { $regex: /example/ } })

and

db.article_category.find({ all_category_id: 8 })
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mongodb 带条件聚合查找 的相关文章

随机推荐

  • hdf5 Java 库入门

    我正在用 jhdf5 学习 HDF5 我正在 MAC OS X 上工作 酿造安装hdf5 这会将 hdf5 1 10 安装在 usr local Cellar hdf5 中 复制此文件并将其放入 gradle 项目中 https suppo
  • SCons 在 Windows 中启动超慢

    长期以来 我在使用 SCons 进行构建时一直饱受启动时间过长的困扰 在我的旧工作笔记本电脑上 构建最基本的 hello world 示例可能需要长达 60 秒的时间 我刚刚收到一台新笔记本电脑 所以我有机会进一步调查这一点 我们的笔记本电
  • 将 LINQ 表达式谓词从一种类型更改为另一种类型

    我有两个不相关的课程 一种作为 API 公开 另一种由 3rd 方 API 在内部使用 Entity 从我们的 API 公开 而 EntityProvider 来自第 3 方程序集 class Entity public A get set
  • stringstream 到底做了什么?

    我从昨天开始尝试学习 C 我正在使用这个文档 http www cplusplus com files tutorial pdf 第 32 页 我在文档中找到了一段代码并运行了它 我尝试输入价格为 5 5 卢比 数量为整数 但输出为 0 我
  • Android - 如何使标题(TextView)在工具栏中居中?

    在我的工具栏左侧有一个徽标 我想将 TextView 放在同一个工具栏中作为我的标题 由于某种原因 标题没有居中 而是位于右侧 这是我的尝试
  • 使用 Perl / AWK 将两连续行合并为一行

    我有如下数据 abcd join abcd efgh join efgh 我想将两个连续的对连接成一行 结果 abcd join abcd efgh join efgh 我怎样才能在 Perl AWK 中做到这一点 sed N s n in
  • Crystal Report 可重新分发用于 Windows Server 2008? - 尝试加载 Crystal Reports 运行时时发生错误

    首先 谢谢大家 我需要什么才能在 Windows Server 2008 上获取工作 Crystal Report 或者我如何在 Windows Server 2008 上运行的网站 框架 3 5 4 0 上获取工作 rpt 文件 由代码加
  • .htaccess 将子域重定向到域

    我有一个子域http sub domain com 我希望来自该子域的所有链接都重定向到目标域 与 www 非 www 以及重定向路径 重定向 www http www sub domain com 非万维网 http sub domain
  • 电子邮件中支持 JavaScript 吗?

    电子邮件中支持 JavaScript 吗 http en wikipedia org wiki Comparison of e mail clients 老客户 例如 Lotus Notes Mozilla Thunderbird Outl
  • Visual Studio 2008 中的“文件另存为”对话框问题

    我正在通过 Visual Studio 2008 进行编程 我更改了 html 页面并使用 Ctrl S 保存它 然后在浏览器上按 Ctrl F5 进行测试 我的问题是 当我在刷新浏览器后将文件保存在 VS 中时 会出现 文件另存为 对话框
  • 使用php在浏览器中显示.msg文件

    我已在 php 应用程序中附加了 Outlook 消息文件 我将该文件存储在 SQL Server 数据库中 现在我想从浏览器打开并显示它 我尝试了这段代码 if ext msg header ContentType application
  • 如何使用 jQuery 获取整个页面的 HTML?

    I used document html 但这引发了错误 有没有办法获得一切 你可以尝试 html html 如果您还想捕获 html 标签 您可以将它们连接到 html 如下所示 function getPageHTML return h
  • pandas 如何在时间序列数据上“get_dummies”

    如果我有一些时间序列数据 弥补一些 import numpy as np import pandas as pd np random seed 11 rows cols 50000 2 data np random rand rows co
  • 使用 segue 设置详细视图控制器

    背景 我有一个自定义 UIViewController 类 我在其中使用自定义注释填充 MKMapView 当用户选择注释时 会显示有关该注释的详细信息 并且还会显示一个按钮 供用户选择并调出另一个 UIViewController 其中包
  • 重播非循环 gif 图像

    我有一个非循环 gif 我将其用作两个 JLabel 的 ImageIcon 但不同时使用 我的问题是 当我将第二个 JLabel 的图标设置为 gif 时 动画已经播放过 因此它只显示最后一帧 您知道当 gif 设置为第二个 JLabel
  • VBA 中的哨兵对象

    我在网络上和我自己的项目中普遍看到具有以下模式的代码 Sub Func Application EnableEvents False some code Application EnableEvents True End Sub 自从有生以
  • 我自己的驱动程序是否需要 Windows 7 x64 中的数字签名

    我已经创建了驱动程序来在 Windows 7 x64 上挂接 ssdt 我怀疑是否需要对驱动程序进行数字签名才能在 x64 上以内核模式安装 否则我只需要 Windows 驱动套件即可安装它 除了 Windows Driver Kit WD
  • 获取维度长度,C# 数组

    int arr new int 2 5 var rows arr var cols arr Assert Equals 3 rows Assert Equals 6 cols 您可以使用GetLength some dimension st
  • 用于验证 IPv4 和 IPv6 地址(无主机名)的 JavaScript 正则表达式

    请建议一个可以验证的 JS 正则表达式 IPv4地址 IPv6地址 该正则表达式应该只验证地址而不验证主机名 我有完全相同的需要 所以我改编了正则表达式丹尼尔的精彩回答 这是我能找到的最准确的 不验证主机名 这里是 var expressi
  • Mongodb 带条件聚合查找

    我有一个名为article category其中存储所有article id属于以下类别category id像这样的数据格式 集合 1 article category article id 2015110920343902 all ca