无法识别的表达式' $ getfield'

发布于 2025-01-17 17:51:02 字数 5236 浏览 1 评论 0原文

我正在尝试使用对象中的值添加新字段:

db.requests.aggregate([
  {
    "$match": {
      "$and": [
        {
          tag: "prod",
          status: "OK",
          created_at: {
            $gte: ISODate("2021-01-01"),
            $lt: ISODate("2022-01-01")
          }
        },
        
      ]
    }
  },
  {
    "$addFields": { "fields": {"$objectToArray": "$fields"} }
  },
  {
    "$unwind": "$fields"
  },
  {
    "$addFields": { "fname": {"$getField": { "field": "k", "input": "$fields" }}, <---- HERE
                    "fconf": {"$getField": { "field": "confidence", "input": {"$getField": { "field": "v", "input": "$fields" }}}}
    }
  },
  {
    $group: {
      _id: "$document",
      "fname": {"$sum": {
        "$cond": {
          "if": {"$gte": [ "$fconf", 0.99 ]},
          "then": 1,
          "else": 0 }
      }},
    }
  }
])

但出现错误:

"ok" : 0,
"errmsg" : "Invalid $addFields :: caused by :: Unrecognized expression '$getField'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"

我的 mongo 版本是 5.0.4,$getField 是在 5.0.0 中引入的,那么出了什么问题?

更新: 样本数据

{ "_id" : ObjectId("610bcecea7f31aaa40b08186"), "tag" : "prod", "document" : "RUT", "fields" : { "9-1-1-1" : { "text" : "1018462101", "confidence" : 1 }, "9-1-1-2" : { "text" : "32", "confidence" : 1 }, "9-1-1-3" : { "text" : "*******", "confidence" : 0.99 }, "9-1-1-4" : { "text" : "*******", "confidence" : 0.99 }, "9-1-1-5" : { "text" : "*******", "confidence" : 0.99 }, "9-1-1-6" : { "text" : "CATALINA", "confidence" : 0.99 }, "9-1-1-7" : { "text" : "8299", "confidence" : 1 } }, "status" : "OK", "created_at" : ISODate("2021-08-05T11:43:10.110Z") }
{ "_id" : ObjectId("610bced6a7f31aaa40b08187"), "tag" : "prod", "document" : "FNA", "fields" : { "20-20-4-4-1-1-1-1-2-1" : { "text" : "", "confidence" : 0 }, "20-20-4-4-1-1-1-1-3-1" : { "text" : "", "confidence" : 0 }, "20-20-4-4-1-1-1-1-4-1" : { "text" : "", "confidence" : 0 }, "20-20-4-4-1-1-1-1-5-1" : { "text" : "", "confidence" : 0 } }, "status" : "OK", "created_at" : ISODate("2021-08-05T11:43:18.706Z") }
{ "_id" : ObjectId("610bcef70b1451dacf81bf32"), "tag" : "prod", "document" : "TAX", "fields" : { "20-19-5-1-2-1-1-1-2-1" : { "text" : "6729000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-3-1" : { "text" : "7443000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-4-1" : { "text" : "54895000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-5-1" : { "text" : "16582000", "confidence" : 0.99 }, "20-19-5-1-2-1-1-1-6-1" : { "text" : "16582000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-7-1" : { "text" : "36083000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-8-1" : { "text" : "2000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-9-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-10-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-11-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-12-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-13-1" : { "text" : "2000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-14-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-15-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-16-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-17-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-18-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-19-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-20-1" : { "text" : "52667000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-21-1" : { "text" : "16582000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-22-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-23-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-24-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-25-1" : { "text" : "36085000", "confidence" : 0.99 }, "20-19-5-1-2-1-1-1-26-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-27-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-28-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-29-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-30-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-31-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-32-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-33-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-34-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-35-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-36-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-37-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-38-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-39-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-40-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-41-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-42-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-43-1" : { "text" : "0", "confidence" : 1 } }, "status" : "OK", "created_at" : ISODate("2021-08-05T11:43:51.466Z") }

预期结果: 我需要像 {_id: "RUT", fields: {9-1-1-1: 10, 9-1-1-2: 5}} 因此,我需要聚合所有内容,并按置信度大于 0.99 的每个字段的名称进行计数。可以有许多文档,每个文档都有不同的字段集,我需要首先按 document 对它们进行分组,然后按照我上面描述的过程在这种类型的许多文档之间进行计数

I am trying to add new field using the value from object:

db.requests.aggregate([
  {
    "$match": {
      "$and": [
        {
          tag: "prod",
          status: "OK",
          created_at: {
            $gte: ISODate("2021-01-01"),
            $lt: ISODate("2022-01-01")
          }
        },
        
      ]
    }
  },
  {
    "$addFields": { "fields": {"$objectToArray": "$fields"} }
  },
  {
    "$unwind": "$fields"
  },
  {
    "$addFields": { "fname": {"$getField": { "field": "k", "input": "$fields" }}, <---- HERE
                    "fconf": {"$getField": { "field": "confidence", "input": {"$getField": { "field": "v", "input": "$fields" }}}}
    }
  },
  {
    $group: {
      _id: "$document",
      "fname": {"$sum": {
        "$cond": {
          "if": {"$gte": [ "$fconf", 0.99 ]},
          "then": 1,
          "else": 0 }
      }},
    }
  }
])

But getting error:

"ok" : 0,
"errmsg" : "Invalid $addFields :: caused by :: Unrecognized expression '$getField'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"

My mongo version is 5.0.4, $getField was introduced in 5.0.0, so what's wrong?

UPDATE:
Sample Data

{ "_id" : ObjectId("610bcecea7f31aaa40b08186"), "tag" : "prod", "document" : "RUT", "fields" : { "9-1-1-1" : { "text" : "1018462101", "confidence" : 1 }, "9-1-1-2" : { "text" : "32", "confidence" : 1 }, "9-1-1-3" : { "text" : "*******", "confidence" : 0.99 }, "9-1-1-4" : { "text" : "*******", "confidence" : 0.99 }, "9-1-1-5" : { "text" : "*******", "confidence" : 0.99 }, "9-1-1-6" : { "text" : "CATALINA", "confidence" : 0.99 }, "9-1-1-7" : { "text" : "8299", "confidence" : 1 } }, "status" : "OK", "created_at" : ISODate("2021-08-05T11:43:10.110Z") }
{ "_id" : ObjectId("610bced6a7f31aaa40b08187"), "tag" : "prod", "document" : "FNA", "fields" : { "20-20-4-4-1-1-1-1-2-1" : { "text" : "", "confidence" : 0 }, "20-20-4-4-1-1-1-1-3-1" : { "text" : "", "confidence" : 0 }, "20-20-4-4-1-1-1-1-4-1" : { "text" : "", "confidence" : 0 }, "20-20-4-4-1-1-1-1-5-1" : { "text" : "", "confidence" : 0 } }, "status" : "OK", "created_at" : ISODate("2021-08-05T11:43:18.706Z") }
{ "_id" : ObjectId("610bcef70b1451dacf81bf32"), "tag" : "prod", "document" : "TAX", "fields" : { "20-19-5-1-2-1-1-1-2-1" : { "text" : "6729000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-3-1" : { "text" : "7443000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-4-1" : { "text" : "54895000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-5-1" : { "text" : "16582000", "confidence" : 0.99 }, "20-19-5-1-2-1-1-1-6-1" : { "text" : "16582000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-7-1" : { "text" : "36083000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-8-1" : { "text" : "2000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-9-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-10-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-11-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-12-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-13-1" : { "text" : "2000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-14-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-15-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-16-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-17-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-18-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-19-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-20-1" : { "text" : "52667000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-21-1" : { "text" : "16582000", "confidence" : 1 }, "20-19-5-1-2-1-1-1-22-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-23-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-24-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-25-1" : { "text" : "36085000", "confidence" : 0.99 }, "20-19-5-1-2-1-1-1-26-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-27-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-28-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-29-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-30-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-31-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-32-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-33-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-34-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-35-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-36-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-37-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-38-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-39-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-40-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-41-1" : { "text" : "0", "confidence" : 1 }, "20-19-5-1-2-1-1-1-42-1" : { "text" : "0", "confidence" : 0.7 }, "20-19-5-1-2-1-1-1-43-1" : { "text" : "0", "confidence" : 1 } }, "status" : "OK", "created_at" : ISODate("2021-08-05T11:43:51.466Z") }

Expected result:
I need sth like {_id: "RUT", fields: {9-1-1-1: 10, 9-1-1-2: 5}}
So i need to aggregate everything and count each field by name that has confidence more that 0.99. There can be many documents each with different set of fields, I need first to group them by document, than count between many documents of this type the procedure that I described above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2025-01-24 17:51:02

在前三个步骤之后,我们可以在 $group 之前使用 $match 这使事情变得更简单,并将 $addFields 步骤移到它后面(此处为 $project)。然后只需再次 $group$project 即可获得想要的结果:

    db.collection.aggregate([
  {
    "$match": {
      "$and": [
        {
          tag: "prod",
          status: "OK",
          created_at: {
            $gte: ISODate("2021-01-01"),
            $lt: ISODate("2022-01-01")
          }
        },
        
      ]
    }
  },
  {
    "$addFields": {"fields": {"$objectToArray": "$fields"}}
  },
  {
    "$unwind": "$fields"
  },
  {
    $match: {"fields.v.confidence": {$gte: 0.99}}
  },
  {
    $group: {
      _id: {
        doc: "$document",
        fname: "$fields.k"
      },
      "dataK": {
        $first: "$fields.k"
      },
      "dataV": {
        $sum: 1
      }
    }
  },
  {
    $project: {"data.k": "$dataK", "data.v": "$dataV"}
  },
  {
    $group: {
      _id: "$_id.doc",
      fields: {$push: "$data"}
    }
  },
  {
    $project: {"fields": {"$arrayToObject": "$fields"}}
  }
])

可以看到 这里在操场上,有一个额外的文档,演示了字段的计数。

After your first three steps, we can use a $match before your $group which makes things simpler, and move the $addFields step after it (here as $project). Then it is just a matter of $group again and $project to get the wanted results:

    db.collection.aggregate([
  {
    "$match": {
      "$and": [
        {
          tag: "prod",
          status: "OK",
          created_at: {
            $gte: ISODate("2021-01-01"),
            $lt: ISODate("2022-01-01")
          }
        },
        
      ]
    }
  },
  {
    "$addFields": {"fields": {"$objectToArray": "$fields"}}
  },
  {
    "$unwind": "$fields"
  },
  {
    $match: {"fields.v.confidence": {$gte: 0.99}}
  },
  {
    $group: {
      _id: {
        doc: "$document",
        fname: "$fields.k"
      },
      "dataK": {
        $first: "$fields.k"
      },
      "dataV": {
        $sum: 1
      }
    }
  },
  {
    $project: {"data.k": "$dataK", "data.v": "$dataV"}
  },
  {
    $group: {
      _id: "$_id.doc",
      fields: {$push: "$data"}
    }
  },
  {
    $project: {"fields": {"$arrayToObject": "$fields"}}
  }
])

Can be seen here on the playground, with one extra document, demonstrating the count of fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文