简体   繁体   English

MongoDB 如何使用 C# 驱动程序在一个查询中进行多个聚合?

[英]MongoDB how to do multiple aggregations in one query with C# driver?

My data model looks something like this:我的数据模型如下所示:

{
    "ListingId": 1,
    "Status": "Current",
    "Category": "Rental"
}

I have around 5 million documents, and would like to run aggregations on both the "status" and "category" fields, so i can produce an output like this:我有大约 500 万个文档,并且希望在“状态”和“类别”字段上运行聚合,因此我可以生成如下输出:

{
   "Statuses": 
   {
      "Current": 100, // count of all documents with 'Status' = 'Current'
      "Sold": 12 // count of all documents with 'Status' = 'Sold'
      // etc
   },
   "Categories": 
   {
      "Sale": 120,
      "Rental": 12
      // etc
   }
}

Basically, i want to return data to power a 'faceted search' UI, so i need the count's of all facets, and I want to do it in one trip to MongoDB.基本上,我想返回数据以支持“分面搜索”用户界面,因此我需要所有方面的计数,并且我想在一次 MongoDB 之旅中完成。

I've been reading up about Aggregations and Facets for a day or so now, and i'm still confused.我已经阅读了一天左右的聚合和构面,但我仍然感到困惑。

Questions:问题:

  1. Given my scenario and collection size, what should i be using here?鉴于我的场景和集合大小,我应该在这里使用什么? Facets or Aggregations?方面还是聚合?
  2. How do i do it in the latest C# driver?我如何在最新的 C# 驱动程序中做到这一点?

Related questions (which i haven't been able to convert to my situation / code):相关问题(我无法转换为我的情况/代码):

You can get these data in a little bit different way with aggregation like below.您可以通过如下聚合以稍微不同的方式获取这些数据。 Try facet code like below in my screenshot.在我的屏幕截图中尝试如下面的代码。 Let me know if more help is required.如果需要更多帮助,请告诉我。

多面结果

You can export the same pipeline and put it in your c# code.您可以导出相同的管道并将其放入您的 c# 代码中。

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

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