Retrieve Item List by Checking Multiple Attribute Values in MongoDB in Go
Problem:
Given a JSON data structure with a nested list of venues and their attributes, how do you retrieve the count of specific attributes (e.g., linux users) for multiple venues? For instance, you want to count the number of linux users for venues with IDs 'VID1212' and 'VID4343'.
Solution:
To achieve this in MongoDB, you can utilize the aggregation framework:
For a more flexible alternative:
Replace the last aggregation stage with:
{ "$group": { "_id": null, "counts": { "$push": { "name": "$_id", "count": "$count" } } } }
This alternative groups based on the attribute name and pushes the corresponding count into an array.
For implementation in Golang using mGo (v2), refer to the guidance on http://godoc.org/labix.org/v2/mgo#Collection.Pipe.
The above is the detailed content of How to Efficiently Count Specific Attributes Across Multiple Venues in MongoDB using Go?. For more information, please follow other related articles on the PHP Chinese website!