使用Mongo 3.2.9将转换字符串聚合到Int

dis*_*ive 8 mongodb

我遇到的问题是从字符串转换为整数以创建平均值.我知道在以后的版本中使用$ 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"}}}} ])
Run Code Online (Sandbox Code Playgroud)

我收到以下回复:

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 :
Run Code Online (Sandbox Code Playgroud)

小智 6

由于 $toInt 和 $convert 运算符是 mongodb 4.0 中才添加的,因此最好更改数据库中的字段类型并在之后进行查询。