【发布时间】:2021-01-11 23:30:01
【问题描述】:
我正在尝试编写一个 mongo 管道来选择 mongodb 文档,如下面的函数所示。 mongo.Pipeline 显示“复合文字中缺少类型”。 我正在尝试根据最高容量聚合与 nfType 匹配的文档中的所有 ipv4Addresses。我正在使用 mongo 驱动程序。 谁能帮我解决 mongo.Pipeline 问题。
func (m *NfInstanceDataAccess) FindNFinstanceInfoUsingTACWithCapacity(preferredNfInstances string, tac string) ([]doc, bool) {
var nfInstance []doc
pipeline := mongo.Pipeline{{{"$match", bson.M{{"nfType", preferredNfInstances}}}},
{{"$unwind", "$ipv4Addresses"}},
{{"$group", bson.M{
{"_id": "$capacity"},
{"ipv4Addresses": bson.M{
{"$addToSet", "$ipv4Addresses"},
}},
}}},
{{"$sort", bson.M{
{"_id", 1},
}}},
{{"$limit", 1}},
}
cursor, err := db.Collection(COLLECTION).Aggregate(context.Background(), pipeline)
defer cursor.Close(context.Background())
err = cursor.All(ctx, &nfInstance)
if err != nil {
log.Printf("Unable to perform NRF discovery search: %s", err.Error())
}
if err := cursor.Err(); err != nil {
log.Printf("Cursor error in NRF discovery search: %s", err.Error())
}
return nfInstance, true
}
下面的管道使用 mongo 命令行工作。我想要一个 bson.M 形式的它。任何帮助。
db.nnfdb.aggregate([
{
"$match": {
"nfType": "AMF"
}
},
{
"$unwind": "$ipv4Addresses"
},
{
$group: {
"_id": "$capacity",
"ipv4Addresses": {
"$addToSet": "$ipv4Addresses"
}
}
},
{
"$sort": {
"_id": 1
}
},
{
"$limit": 1
}
])
【问题讨论】:
-
bson.M 是一个映射,所以文字看起来像
bson.M{"key":"value"}。您的管道似乎是为bson.D编写的,它使用您拥有的语法。