Azure cosmos 在 MongoDB 查询管道中引发$round错误



我正在使用Azure Cosmos MongoDB并存储API请求日志。我想知道每秒发出多少请求。所以我在将时间戳除以 1000 然后四舍五入后根据时间戳对它们进行分组,然后四舍五入到小数点后 0 位

1593096483234至 1593096483.234 至 1593096483

并按上述分组对提供的文件进行计数,然后按计数顺序进行排序。

查询

db.server_api_logs.aggregate([
{
"$group": {
"_id": "$timestamp"
}
},
{
"$project": {
"summation": {
"$round": {
"$toDouble": {
"$divide": ["$_id",1000]
}
}
}
}
},
{
"$group": {
"_id": "$summation",
"summer": {"$sum": 1}
}
},
{
"$sort": {"summer": -1}
}
]

尝试运行上述查询时收到此错误

Unable to execute the selected commands
Mongo Server error (MongoCommandException): Command failed with error 168 (168): 'unknown operator: $round' on server <server address> 
The full response is:
{ 
"ok" : 0.0, 
"errmsg" : "unknown operator: $round", 
"code" : NumberInt(168), 
"codeName" : "168"
}

它在使用指南针的本地工作完全正常,但在使用 Robo3T 的 Cosmos 上则无法正常工作。我做错了什么吗?

如MongoDB文档中所述,$round运算符是在MongoDB的4.2版本中引入的。

如Azure Cosmos DB文档中所述,目前,Azure Cosmos DB的MongoDBAPI与MongoDB服务器版本3.6兼容。

这就是为什么您的查询适用于本地MongoDB实例(我相信4.2或更高版本(但不适用于Azure Cosmos DB的原因。

最新更新