Mongo汇总查询的Java代码包含$ push

发布于 2025-01-23 13:18:38 字数 1282 浏览 0 评论 0原文

下面是用于Mongo查询的管道片段。以及我要编写的相应的Java代码。 我会遇到“未识别的表达式”错误。有人可以让我知道Java代码出了什么问题吗? 其他管道似乎还不错。问题是$ push。

我使用的是Mongo Driver 3.8.2,

    "$group": {
      "_id": {
        "a": "$_id.a",
        "b": "$_id.b"
      },
      "data": {
        "$push": {
          "c": "$_id.c",
          "customerCount": {
            "$sum": "$customerCount"
          },
          "itemQuantity": {
            "$sum": "$itemQuantity"
          },
          "amount": {
            "$sum": "$amount"
          }
        }
      },
      dataKey: {
        $first: "$_id.c"
      }
    }

下面的Mongo 5版本是上述摘要异常的Java代码

Bson group = new Document("$group", new Document("_id", idDoc).append("data", new Document("$push", pushDoc)).append("dataKey", new Document("$first", "$_id."+orderBy)));

,如下所示。

"com.mongodb.MongoCommandException: Command failed with error 168 (InvalidPipelineOperator): 'Unrecognized expression '$push'' on server localhost:27017. The full response is { \"ok\" : 0.0, \"errmsg\" : \"Unrecognized expression '$push'\", \"code\" : 168, \"codeName\" : \"InvalidPipelineOperator\" }\r\n\tat com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)
    

Below is a pipeline snippet for mongo query. And the corresponding Java code that I'm trying to write.
I'm getting the error 'Unrecognized expression '$push''. Can someone please let me know what is wrong with the Java code?
Other pipelines seem to be fine. Issue is with $push.

I'm using mongo driver 3.8.2 with latest Mongo 5 version

    "$group": {
      "_id": {
        "a": "$_id.a",
        "b": "$_id.b"
      },
      "data": {
        "$push": {
          "c": "$_id.c",
          "customerCount": {
            "$sum": "$customerCount"
          },
          "itemQuantity": {
            "$sum": "$itemQuantity"
          },
          "amount": {
            "$sum": "$amount"
          }
        }
      },
      dataKey: {
        $first: "$_id.c"
      }
    }

Below is the Java code for above snippet

Bson group = new Document("$group", new Document("_id", idDoc).append("data", new Document("$push", pushDoc)).append("dataKey", new Document("$first", "$_id."+orderBy)));

Exception is as shown below.

"com.mongodb.MongoCommandException: Command failed with error 168 (InvalidPipelineOperator): 'Unrecognized expression '$push'' on server localhost:27017. The full response is { \"ok\" : 0.0, \"errmsg\" : \"Unrecognized expression '$push'\", \"code\" : 168, \"codeName\" : \"InvalidPipelineOperator\" }\r\n\tat com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文