首页 文章

Morphia中的MongoDB聚合

提问于
浏览
1

我正在尝试将成功的MongoDB聚合映射到morphia,但我无法获得令人满意的结果 . 我每次都失败但我无法弄明白为什么 . 也许有人可以帮我在morphia中正确地说明聚合 . 我的MongoDB查询如下所示:

db.user.aggregate([{$match: { roles: "MEMBER" }}, {$group:{_id: "$roles", sum:{$sum: "$payments.2039.amount"}}}])

角色是一个数组,聚合工作正常并输出:

{ "_id" : [ "MEMBER" ], "sum" : 100 }

我尝试使用此java代码在morphia中执行此操作:

final Query<User> query = datastore.createQuery(User.class).field("roles").in(Lists.newArrayList(Role.MEMBER));
final Iterator<AggregatePayments> aggregatePayments = datastore
    .createAggregation(User.class)
    .match(query)
    .group("$roles", grouping("sum", sum("payments." + currentSeason + ".amount")))
    .out(AggregatePayments.class);

但是,不幸的是,这失败了以下例外:

com.mongodb.MongoCommandException: Command failed with error 17276 (Location17276): 'Use of undefined variable: roles' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Use of undefined variable: roles", "code" : 17276, "codeName" : "Location17276" }

我的问题是现在找出为什么它在MongoDB中工作但在morphia中不起作用 . 当我试图在morphia中为_id的roles变量省略“$”时,我得到以下异常:

com.mongodb.MongoCommandException: Command failed with error 16996 (Location16996): 'insert for $out failed: { connectionId: 1419, err: "can't use an array for _id", code: 2, codeName: "BadValue", n: 0, ok: 1.0 }' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "insert for $out failed: { connectionId: 1419, err: \"can't use an array for _id\", code: 2, codeName: \"BadValue\", n: 0, ok: 1.0 }", "code" : 16996, "codeName" : "Location16996" }

任何帮助赞赏!非常感谢你!

使用MongoDB版本3.6和当前morphia构建1.4-SNAPSHOT .

EDIT: 对我来说真正奇怪的是MongoDB中的查询上面有效,但是吗啡生成的查询没有 . 但MongoDB中手动编辑的查询和morphia生成的查询似乎与另一个密切对应 . 有没有人看到任何错误?生成的查询如下:

11040 [qtp104739310-39] DEBUG org.mongodb.driver.protocol.command  - Sending command '{ "aggregate" : "user", "pipeline" : [{ "$match" : { "roles" : { "$in" : ["MEMBER"] } } }, { "$group" : { "_id" : "$roles", "sum" : { "$sum" : "$payments.2039.amount" } } }, { "$out" : "AggregatePayments" }], "cursor" : { }, "$db" : "sua", "$readPreference" : { "mode" : "primaryPreferred" } }' with request id 19 to database sua on connection [connectionId{localValue:2, serverValue:1419}] to server localhost:27017

并产生例外:

com.mongodb.MongoCommandException: Command failed with error 16996 (Location16996): 'insert for $out failed: { connectionId: 1419, err: "can't use an array for _id", code: 2, codeName: "BadValue", n: 0, ok: 1.0 }' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "insert for $out failed: { connectionId: 1419, err: \"can't use an array for _id\", code: 2, codeName: \"BadValue\", n: 0, ok: 1.0 }", "code" : 16996, "codeName" : "Location16996" }

当在MongoDB中手动编辑查询时,我也可以使用$ group:{_ id:“$ roles”....对我来说似乎很奇怪......

1 回答

  • 0

    我没有意识到morphia的“out()”和“aggregate()”方法之间的区别 . “out()”更改MongoDB存储中的集合,这是非预期的 . 使用“aggregate()”现在解决了所有问题:

    final Query<User> query = datastore.createQuery(User.class).field("roles").in(Lists.newArrayList(Role.MEMBER));
    final Iterator<AggregatePayments> aggregatePayments = datastore
        .createAggregation(User.class)
        .match(query)
        .group("roles", grouping("sum", sum("payments." + currentSeason + ".amount")))
        .aggregate(AggregatePayments.class);
    

    现在正如预期的那样使用morphia 1.4.0-SNAPSHOT(最近从git master构建)和MongoDB 3.6 .

相关问题