繁体   English   中英

根据MongoDB中一个文档的另一文档的属性获取结果

[英]Get result based on property of another document from one document in MongoDB

我有两个文件——

const taskSchema = new mongoose.Schema({
    leadId: { type: String, default: '' },
    customer_name: { type: String, default: '' }
})

const leadSchema = new mongoose.Schema({
  Id: { type: String, default: "" },
  associate_status: { type: Boolean, default: false },
})

我想对任务运行查询,以便获得关联状态为真的所有潜在客户。

任务架构中的LeadId与潜在客户架构中的Id相同。

我试过这个 -

  const report = await taskModel.aggregate([
   { $lookup: {
      from: 'leads',
      let: { leadId: '$leadId' },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: ['$Id', '$$leadId'],
            },
          },
        },
      ],
      as: 'leaditems',
    },
  }
  { $unwind: '$leaditems' },
  { $match: { leaditems.associate_status: true}}
 ])

但是由于我有大量文档(超过 200k 潜在客户和 100k 任务,并且这些数字将继续增长),查询有时会在 9 到 10 秒后运行,有时根本不运行。 我正在寻找一种更优化的方法或任何其他可以使这项工作发挥作用的实现。

更新:

我也试过这个,但没有改善 -

const report = await taskModel.aggregate([
       { $lookup: {
          from: 'leads',
          let: { leadId: '$leadId' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [
                   {
                     $eq: ['$Id', '$$leadId'],
                   },
                   {
                     $eq: ['$associate_status', true],
                   },
                 ],
               },
              },
            },
          ],
          as: 'leaditems',
        },
      }
     ])

$match条件放在$lookup pipeline ,甚至尝试将其放在$lookup之前。

在两个关联的 id 上设置index

也许你的服务有一个 10 秒的数据库连接限制,试着让它更长。

要在查找管道内使用$exp的索引,您需要 MongoDB 5。MongoDB 4.4.9 不会使用索引,查询会很慢。

  • 在 Leads 和 Id 中创建索引,预计速度会更快
    (如果索引具有良好的选择性,它会有所帮助,如果选择一小部分文档,将返回 90% 文档的索引会使事情变得更糟)(如果您在 LeadId 索引上添加许多值也将被更新,它会花费,但是大多数时候的好处更多)
  • 替换为简单的相等$lookup
  • 此外,如果您真的不需要$unwind并且您更喜欢数组中的数据,则可以使用$filter
const report = await taskModel.aggregate([
       { $lookup: {
          from: 'leads',
          localField: "leadId",
          foreignField: "Id",
          as : "leaditems"
       },
       { $unwind: '$leaditems' },
       { $match: { leaditems.associate_status: true}}       
     ])

如果可能的话,在你完成这些并发送一些反馈后对其进行基准测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM