首页 > 后端开发 > Golang > 正文

如何使用 Go 在 MongoDB 中选择特定场所 ID 来检索所有 Linux 用户的计数?

Linda Hamilton
发布: 2024-10-26 02:27:02
原创
549 人浏览过

How can I Retrieve the Count of All Linux Users by Selecting Specific Venue IDs in MongoDB using Go?

通过在 Go 中检查 MongoDB 中的多个属性值来检索项目列表

问题:

通过识别多个来检索所选项目MongoDB 中的条件,类似于 MySQL 中的 IN 条件:

<code class="sql">SELECT * FROM venuelist WHERE venueid IN (venueid1, venueid2)</code>
登录后复制

考虑 JSON 结构:

<code class="json">{
  "_id" : ObjectId("57f940c4932a00aba387b0b0"),
  "tenantID" : 1,
  "date" : "2016-10-09 00:23:56",
  "venueList" : [
    {
      "id" : "VID1212",
      "sum" : [
        {
          "name" : "linux",
          "value" : 12
        },
        {
          "name" : "ubuntu",
          "value" : 4
        }
      ],
      "ssidList" : [
        {
          "id" : "SSID1212",
          "sum" : [
            {
              "name" : "linux",
              "value" : 8
            },
            {
              "name" : "ubuntu",
              "value" : 6
            }
          ],
          "macList" : [
            {
              "id" : "12:12:12:12:12:12",
              "sum" : [
                {
                  "name" : "linux",
                  "value" : 12
                },
                {
                  "name" : "ubuntu",
                  "value" : 1
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "id" : "VID4343",
      "sum" : [
        {
          "name" : "linux",
          "value" : 2
        }
      ],
      "ssidList" : [
        {
          "id" : "SSID4343",
          "sum" : [
            {
              "name" : "linux",
              "value" : 2
            }
          ],
          "macList" : [
            {
              "id" : "43:43:43:43:43:34",
              "sum" : [
                {
                  "name" : "linux",
                  "value" : 2
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}</code>
登录后复制

任务:通过选择场地 ID 'VID1212 检索所有 Linux 用户的计数' 和 'VID4343'。

解决方案:

  1. 使用聚合框架使用 $match 根据venueList ID 过滤文档。
  2. 使用 $unwind 展开 venueListsum 子文档数组。
  3. 使用 $match 获取所需的 ID。
  4. 使用
  5. $group** 聚合过滤后的文档,使用 **$sum 累加器获取所需的值。
  6. 使用三元运算符的条件 (
  7. $cond) 创建独立的计数字段。
<code class="go">// Import the necessary packages.
import (
    "context"
    "fmt"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

// retrieveItemListByAttributeValues retrieves items based on multiple attribute values in MongoDB.
func retrieveItemListByAttributeValues(client *mongo.Client) {
    // Create a new context.
    ctx := context.Background()

    // Define the pipeline.
    pipeline := mongo.Pipeline{
        {{"$match", bson.D{{"venueList.id", bson.D{{"$in", bson.A{"VID1212", "VID4343"}}}}}}},
        {{"$unwind", "$venueList"}},
        {{"$match", bson.D{{"venueList.id", bson.D{{"$in", bson.A{"VID1212", "VID4343"}}}}}}},
        {{"$unwind", "$venueList.sum"}},
        {
            {"$group", bson.D{
                {"_id", nil},
                {"linux", bson.D{{"$sum", bson.M{"$cond", bson.A{bson.VC.Bool(true), "$venueList.sum.value", 0}}}}}},
                {"ubuntu", bson.D{{"$sum", bson.M{"$cond", bson.A{bson.VC.Bool(true), "$venueList.sum.value", 0}}}}}},
            }},
        },
    }

    // Execute the aggregation pipeline.
    aggRes, err := client.Database("test").Collection("venuelist").Aggregate(ctx, pipeline)
    if err != nil {
        panic(err)
    }

    // Iterate over the results.
    for aggRes.Next(ctx) {
        var result bson.M
        if err := aggRes.Decode(&result); err != nil {
            panic(err)
        }

        // Print the result.
        fmt.Println(result)
    }

    // Close the cursor.
    if err := aggRes.Close(ctx); err != nil {
        panic(err)
    }
}</code>
登录后复制

替代解决方案:

为了提高性能和灵活性,请考虑以下内容替代管道:

以上是如何使用 Go 在 MongoDB 中选择特定场所 ID 来检索所有 Linux 用户的计数?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!