【问题标题】:How to write mongo pipeline如何编写mongo管道
【发布时间】: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 编写的,它使用您拥有的语法。

标签: mongodb go


【解决方案1】:

mongo.Pipeline 显示“复合文字中缺少类型”

您的聚合管道混淆了bson.Dbson.M 的语法。例如,bson.D 是:

bson.D{{ "a", 1 }, { "b", true }}

bson.M 如下:

bson.M{ "a": 1, "b": true }

请注意括号 {} 的数量以及逗号和分号。

对于有时顺序很重要的 MongoDB 聚合管道,建议使用 bson.D 而不是 bson.M 更安全。使用这些知识,下面是您提供的固定聚合管道。

pipeline := mongo.Pipeline{
    {{"$match", bson.D{{"nfType", preferredNfInstances}}}},
    {{"$unwind", "$ipv4Addresses"}},
    {{"$group", bson.D{
        {"_id", "$capacity"},
        {"ipv4Addresses", bson.D{
            {"$addToSet", "$ipv4Addresses"},
        }},
    }}},
    {{"$sort", bson.D{
        {"_id", 1},
    }}},
    {{"$limit", 1}},
}

或者,如果您想使用bson.M,请参见以下示例:

pipeline := []bson.M{
    bson.M{"$match": bson.M{"nfType": preferredNfInstances}},
    bson.M{"$unwind": "$ipv4Addresses"},
    bson.M{"$group": bson.M{
        "_id": "$capacity",
        "ipv4Addresses": bson.M{
            "$addToSet": "$ipv4Addresses",
        },
    }},
    bson.M{"$sort": bson.M{
        "_id": 1,
    }},
    bson.M{"$limit": 1},
}

【讨论】:

  • 您好,感谢您的回复,但我想要管道线的 bson.M 形式。我已经粘贴了使用 mongo 命令行工作的聚合。你能帮忙写一下它的 bson.M 形式吗?
  • 嗨@samcreg,我也添加了bson.M 的示例。以后,请记住,如果您在 StackOverflow 中提出问题,您应该伴随着您尝试过的尝试。
  • 谢谢!我会这样做的。
猜你喜欢
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
相关资源
最近更新 更多