【问题标题】:MongoDB - Load a subset of documents for queryingMongoDB - 加载文档子集以进行查询
【发布时间】:2016-05-10 19:44:05
【问题描述】:

我有一个包含多个客户、月份等的集合。

每个月我都需要对集合运行一系列查询以检索指标 - 基本上是一堆计数。

这些查询中的大多数都有相同的 4 或 5 个过滤器,另外还有 1-2 个过滤器会随每个查询而变化。

//standard filters:

{ client: "ABC Corp",
  environment: "Production",
  device: "true",
  registered: "true"
}

//special filters:

{ type: "typeA",
  screen: "screen1"   }

{ type: "typeA",
  screen: "screen2"   }

在 MSSQL 中,我将创建一个包含 4 个标准过滤器的视图,并重复查询该视图,应用额外的 1-2 个过滤器来检索所需的指标。

关于我可以使用什么 MongoDB 或基本 JS 方法的任何建议?我的目标是避免使用相同的标准过滤器一遍又一遍地点击集合。

谢谢 V

【问题讨论】:

    标签: sql sql-server mongodb nosql


    【解决方案1】:

    您可以在聚合框架中使用 $out 运算符来实现这一点。

    $outstage 可以将结果输出到新的集合中

    例如:

    db.collection.aggregate([
      {
        $match:
          {
            client: "ABC Corp",
            environment: "Production",
            device: "true",
            registered: "true"
          }
      },
      {
        $out: "new_collection"
      }
    ])
    

    然后您可以在 new_collection 上运行其他查询。

    您的“视图”集合每次在 $out 阶段使用时都会重新创建,但您在该集合上定义的任何索引都将保持不变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2013-04-06
      相关资源
      最近更新 更多