无法识别的表达式' $ getfield'
我正在尝试使用对象中的值添加新字段:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在前三个步骤之后,我们可以在
$group
之前使用$match
这使事情变得更简单,并将$addFields
步骤移到它后面(此处为$project
)。然后只需再次$group
和$project
即可获得想要的结果:可以看到 这里在操场上,有一个额外的文档,演示了
字段
的计数。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:Can be seen here on the playground, with one extra document, demonstrating the count of
fields
.