如何在golang中编写bson形式的mongo查询? [英] How to write bson form of mongo query in golang?

查看:213
本文介绍了如何在golang中编写bson形式的mongo查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用命令行查询查询mongodb集合以基于nfType和最小距离来获取ipv4Addresses

I can query my mongodb collection to get the ipv4Addresses based on the nfType and the minimum distance using the command line query

db.nfinstancesdb.aggregate([
  {
    "$match": {
      "nfType": "AMF"
    }
  },
  {
    "$unwind": "$ipv4Addresses"
  },
  {
    $group: {
      "_id": "$distance",
      "ipv4Addresses": {
        "$addToSet": "$ipv4Addresses"
      }
    }
  },
  {
    "$sort": {
      "_id": 1
    }
  },
  {
    "$limit": 1
  }
])

这使输出预期为

[{"_id": 10,"ipv4Addresses": ["172.16.0.11","172.16.0.10"]}]

如何在Go上编写上述查询的bson形式?

How can I write the bson form of the above query on Go?

我在下面的函数中进行了操作,但正在获取所有的ipv4Addresses 而不是上面的结果.

I did in the function below but am getting all the ipv4Addresses instead of the above result.

func (m *NfInstanceDataAccess) FindIp(nfType string) ([]NfInstance, error) {
    var ip []NfInstance

    collection := db.C(COLLECTION)
    pipeline := mongo.Pipeline{
        {{"$match", bson.D{
            {"nfType", "AMF"},
        }}},
        {{"$unwind", "$ipv4Addresses"}},
        {{"$group", bson.D{
            {"_id", "$distance"},
            {"ipv4Addresses", bson.D{
                {"$addToSet", "$ipv4Addresses"},
            }},
        }}},
        {{"$sort", bson.D{
            {"_id", 1},
        }}},
        {{"$limit", 1}},
    }

    cursor, err := collection.Aggregate(context.Background(), pipeline)
    defer cursor.Close(context.Background())
    for cursor.Next(context.Background()) {
        var ip []NfInstance
        err := cursor.Decode(&ip)
        if err != nil {
            log.Fatal(err)
        }
        //fmt.Println(doc)
    }
    return ip, nil
}


我的收藏有以下物品

{
    "nfInstanceID": "1",
    "nfType": [
      "AMF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.10"
    ],
    "distance": 10
  },
  {
    "nfInstanceID": "2",
    "nfType": [
      "UPF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.20"
    ],
    "distance": 20
  },
  {
    "nfInstanceID": "3",
    "nfType": [
      "AMF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.30"
    ],
    "distance": 30
  },
  {
    "nfInstanceID": "4",
    "nfType": [
      "AMF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.11"
    ],
    "distance": 10
  }

我期望输出相同或相似.

And I am expecting the same or similar output.

推荐答案

golang代码存在的问题是您没有分组.

The problem with your golang code is you didn't group.

您可以使用Pipe来准备要聚合的管道:

You can make use of Pipe which prepares a pipeline to aggregate:

pipe := db.C(COLLECTION).Pipe([]bson.M{
    {"$match":  bson.M{"nfType": "AMF"}},
    {"$unwind": "$ipv4Addresses"},
    {"$group":  bson.M{
                       "_id": "$distance",
                       "ipv4Addresses": bson.M{"$addToSet": "$ipv4Addresses"},
                      }},
    {"$sort": bson.M{"_id": 1}},
    {"$limit": 1},

})

err := pipe.All(&ip)

这篇关于如何在golang中编写bson形式的mongo查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆