春季的Mongo查询

时间:2018-11-16 14:15:24

标签: spring mongodb kotlin

查询时

db.inventory.aggregate({"$group": {"_id": null, "avg": {"$avg": "$size.h"}}})

mongo还给我

{ "_id" : null, "avg" : 12.77 }

符合预期。

但是,当我在Spring中使用

实现此查询时
@Query("{\"\$group\": {\"_id\": null, \"avg\": {\"\$avg\": \"\$size.h\"}}}")

我收到以下错误:

{
    "error": "Internal Server Error",
    "message": "Query failed with error code 2 and error message 'unknown top level operator: $group' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'unknown top level operator: $group' on server localhost:27017",
    "path": "/avg",
    "status": 500,
    "timestamp": "2018-11-15T00:44:14.011+0000"
}

为什么会这样?

0 个答案:

没有答案