我遇到的问题是从字符串转换为整数以创建平均值。我知道在以后的版本中会使用$ convert,但是我找不到使用$ toInt术语的正确位置。我知道使用此关键字进行的转换仅在一个示例中就可以在命令行上运行,但是应该将其放置在聚合框架中的什么位置。
db.my_batch.aggregate([{"$unwind": "$current.Data.x"}, {"$match": {"current.Data.x.Typ": "01", "current.Data.x.Value": {"$lt": "TTTT"}}}, {"$project": {"current.Data.x.Value": 1, "uId":1}}, {"$group": {"_id": null, "ad": {"$avg": {"$toInt": "$current.Data.x.Value"}}}} ])
我收到以下答复:
2018-07-20T17:19:42.707+0200 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression '$toInt'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
} : aggregate failed :
答案 0 :(得分:0)
由于$ toInt和$ convert运算符仅在mongodb 4.0中添加,因此最好更改数据库中的字段类型并在此之后查询。