Home  >  Article  >  Backend Development  >  How to Retrieve Item Count Based on Multiple Attribute Values in MongoDB using Aggregation Pipeline?

How to Retrieve Item Count Based on Multiple Attribute Values in MongoDB using Aggregation Pipeline?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 19:20:29459browse

How to Retrieve Item Count Based on Multiple Attribute Values in MongoDB using Aggregation Pipeline?

Retrieve Item List by Checking Multiple Attribute Values in MongoDB

When working with MongoDB, it often becomes necessary to retrieve specific items based on multiple attribute values, similar to the IN condition used in MySQL. This article demonstrates how to achieve this using a combination of pipeline operations in MongoDB.

Aggregation Framework

In this scenario, we'll use the aggregation framework to construct a pipeline that filters and aggregates data from a MongoDB collection known as "venueList." The goal is to retrieve the total count of users with specific operating systems, such as Linux or Ubuntu, based on a list of venue IDs.

Pipeline Stages

The pipeline consists of several stages that work together to transform and summarize the data:

  1. $match: Filters the collection based on the specified venue IDs, ensuring that only relevant documents are considered.
  2. $unwind: Denormalizes the "venueList" and "sum" arrays to create individual documents for each venue and user agent distribution.
  3. $match: Filters the denormalized documents again to ensure that only the desired venue IDs are included.
  4. $unwind: Further denormalizes the "sum" array to create individual documents for each user agent distribution.
  5. **$group:** Aggregates the denormalized documents using the $cond operator to create separate fields for each operating system (e.g., "linux" and "ubuntu"). The $sum operator is used to calculate the total count of users for each operating system.

Expected Output

The final output of the aggregation pipeline will be a document containing the total count of Linux and Ubuntu users across the specified venue IDs:

<code class="json">{
  "_id": null,
  "linux": 14,
  "ubuntu": 4
}</code>

Go Implementation

To use this pipeline in Go using the mgo package, you can follow these steps:

<code class="go">query := []bson.M{
  {"$match": bson.M{"venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}}}},
  {"$unwind": "$venueList"},
  {"$match": bson.M{"venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}}}},
  {"$unwind": "$venueList.sum"},
  {
    "$group": bson.M{
      "_id":        nil,
      "linux":      bson.M{"$sum": bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$venueList.sum.name", "linux"}}, "$venueList.sum.value", 0}}},
      "ubuntu":     bson.M{"$sum": bson.M{"$cond": []interface{}{bson.M{"$eq": []interface{}{"$venueList.sum.name", "ubuntu"}}, "$venueList.sum.value", 0}}},
    },
  },
}</code>

The above is the detailed content of How to Retrieve Item Count Based on Multiple Attribute Values in MongoDB using Aggregation Pipeline?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn