当前位置: > > > > 如何在golang中编写bson形式的mongo查询?
来源:stackoverflow
2024-04-19 23:09:33
0浏览
收藏
大家好,今天本人给大家带来文章《如何在golang中编写bson形式的mongo查询?》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
问题内容
我可以使用命令行查询查询我的 mongodb 集合,以根据 nftype 和最小距离获取 ipv4addresses
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 形式?
我在下面的函数中执行了操作,但获得了所有 ipv4addresses 而不是上面的结果。
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 }
我期待相同或相似的输出。
解决方案
你的golang代码的问题是你没有分组。
您可以使用 pipe
来准备要聚合的管道:
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)
如果您使用官方的 ,您可以利用 来执行 。您发布的示例 go 代码片段使用 find()
,这与聚合不同。
例如,使用 mongodb go 驱动程序 v1.0.4(当前):
collection := client.Database("dbname").Collection("collname") 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()) { doc := bson.D{} err := cursor.Decode(&doc) if err != nil { log.Fatal(err) } fmt.Println(doc) }
您发布的示例文档对于所有 ipv4addresses
只有 1 个元素,我假设这只是一个示例。但是,如果所有文档都只有 1 个 ipv4addresses
元素数组,那么您最好只使用 。
通常,当序列化为 bson 时(以及当顺序很重要时),使用 。
本篇关于《如何在golang中编写bson形式的mongo查询?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注公众号!