简体   繁体   中英

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:

{
   "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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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