我正在尝试将一个成功的MongoDB聚合映射到莫菲亚,但我无法得到令人满意的结果。我每次都失败了,但我不知道为什么。也许你们中的某个人可以帮助我在莫菲亚中正确地陈述聚合。我的MongoDB查询如下所示:
db.user.aggregate([{$match: { roles: "MEMBER" }}, {$group:{_id: "$roles", sum:{$sum: "$payments.2039.amount"}}}])
角色是一个数组,聚合工作正常并输出:
{ "_id" : [ "MEMBER" ], "sum" : 100 }
我试图用这个java代码来做到这一点:
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的“$”at角色变量时,我得到以下异常:
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版和当前moria构建1.4-SNAPSHOT。
编辑:对我来说真正奇怪的是,上面的MongoDB中的查询可以工作,但morfi生成的查询却不能。但是MongoDB中手动编辑的查询和morfi生成的查询似乎非常对应。有人看到任何错误吗?生成的查询如下:
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:"$角色"……对我来说似乎很奇怪…
我没有认识到莫菲亚的“out()”和“聚合()”方法之间的区别。“out()”更改了MongoDB存储中的集合,这不是预期的。使用“聚合()”现在解决了所有问题:
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);
现在可以像预期的那样使用morfia1.4.0-SNAPSHOT(最近从git master构建)和MongoDB 3.6。