[英]Morphia java.util.Arrays$ArrayList cannot be cast to com.mongodb.DBObject when making a Projection.projection
I am trying to put the following aggregation我正在尝试进行以下聚合
db.getCollection("order").aggregate(
[
{
"$project" : {
"_id" : -1.0,
"customerId" : "$customer.customerId",
"hasOrderInT0" : {
"$cond" : [
{
"$and" : [
{
"$gte" : [
"$date",
1577829600.0
]
},
{
"$lte" : [
"$date",
1580507999.0
]
}
]
},
1,
0
]
}
}
}
]
);
in a Java app, where I am using Morphia as ORM.在 Java 应用程序中,我使用 Morphia 作为 ORM。 Basically if the date is between 2 timestamps, it will put 1 in the hasOrderInT0
field and 0 otherwise.基本上,如果日期在 2 个时间戳之间,它将在hasOrderInT0
字段中输入 1,否则为 0。
long initialStart = 1577829600;
long initialEnd = 1580507999;
AggregationPipeline pipeline = databaseService.getConnection().createAggregation(Order.class)
.project(
Projection.projection("_id", "-1"),
Projection.projection("customerId", "$customer.customerId"),
Projection.projection("hasOrderInT0",
Projection.expression(
"$cond",
Arrays.<Object>asList(
new BasicDBObject(
"$and", Arrays.<Object>asList(
new BasicDBObject(
"$gte", Arrays.<Object>asList("$date", initialStart)
),
new BasicDBObject(
"$lte", Arrays.<Object>asList("$date", initialEnd)
)
)
),
1,
0
)
)
)
);
When running the above code, I get the following error:运行上述代码时,出现以下错误:
Caused by: java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to com.mongodb.DBObject
at xyz.morphia.aggregation.AggregationPipelineImpl.toExpressionArgs(AggregationPipelineImpl.java:296)
at xyz.morphia.aggregation.AggregationPipelineImpl.toDBObject(AggregationPipelineImpl.java:249)
at xyz.morphia.aggregation.AggregationPipelineImpl.toDBObject(AggregationPipelineImpl.java:240)
at xyz.morphia.aggregation.AggregationPipelineImpl.project(AggregationPipelineImpl.java:191)
This is my first time using Projection with Morphia and I don't know if this is the right way to implement the command that works in mongo console.这是我第一次将 Projection 与 Morphia 一起使用,我不知道这是否是实现在 mongo 控制台中工作的命令的正确方法。
PS: The $project
is just a pipeline from a bigger aggregate, but this is the part that is of interest and which is giving the error, so I simplified it for demonstration purpose. PS: $project
只是一个更大聚合的管道,但这是感兴趣的部分,它给出了错误,所以为了演示目的,我对其进行了简化。
It turns out there is no need to wrap the condition from $cond into in Arrays.<Object>asList
.事实证明,不需要将 $cond 中的条件包装到Arrays.<Object>asList
中。 Projection.expression already accepts an arbitrary number of arguments, so the working code is: Projection.expression 已经接受了任意数量的 arguments,所以工作代码是:
long initialStart = 1577829600;
long initialEnd = 1580507999;
AggregationPipeline pipeline = databaseService.getConnection().createAggregation(Order.class)
.project(
Projection.projection("_id", "-1"),
Projection.projection("customerId", "$customer.customerId"),
Projection.projection("hasOrderInT0",
Projection.expression(
"$cond",
new BasicDBObject(
"$and", Arrays.<Object>asList(
new BasicDBObject(
"$gte", Arrays.<Object>asList("$date", initialStart)
),
new BasicDBObject(
"$lte", Arrays.<Object>asList("$date", initialEnd)
)
)
),
1,
0
)
)
);
The BasicDBObject is the first arg, 1 and 0 are the 2nd and 3rd and they will be correctly interpreted by Morphia wrapper. BasicDBObject 是第一个参数,1 和 0 是第二个和第三个参数,它们将被 Morphia 包装器正确解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.