进行Projection.Projection时,Morphia java.util.Arrays$ArrayList不能强制转换为com.mongodb.DBObject
我试图把下面的聚合进行Projection.Projection时,Morphia java.util.Arrays$ArrayList不能强制转换为com.mongodb.DBObject,java,mongodb,projection,morphia,Java,Mongodb,Projection,Morphia,我试图把下面的聚合 db.getCollection("order").aggregate( [ { "$project" : { "_id" : -1.0, "customerId" : "$customer.customerId", "hasOrderInT0" : { "$cond" : [
db.getCollection("order").aggregate(
[
{
"$project" : {
"_id" : -1.0,
"customerId" : "$customer.customerId",
"hasOrderInT0" : {
"$cond" : [
{
"$and" : [
{
"$gte" : [
"$date",
1577829600.0
]
},
{
"$lte" : [
"$date",
1580507999.0
]
}
]
},
1,
0
]
}
}
}
]
);
在Java应用程序中,我使用Morphia作为ORM。基本上,如果日期介于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
)
)
)
);
这是我第一次在Morphia中使用投影,我不知道这是否是在mongo控制台中实现命令的正确方法
注:
$project
只是一个更大聚合的管道,但这是一个有趣的部分,它给出了错误,因此为了演示,我对它进行了简化。结果表明,没有必要将$cond的条件包装到数组中。asList
。Projection.expression已接受任意数量的参数,因此工作代码为:
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
)
)
);
long initialStart=1577829600;
长initialEnd=1580507999;
AggregationPipeline=databaseService.getConnection().CreateAgregation(Order.class)
.项目(
投影。投影(“_id”,“-1”),
Projection.Projection(“customerId”,“$customer.customerId”),
Projection.Projection(“hasOrderInT0”,
投影表达式(
“$cond”,
新的基本对象(
“$and”,Arrays.asList(
新的基本对象(
“$gte”,数组.asList(“$date”,initialStart)
),
新的基本对象(
“$lte”,Arrays.asList(“$date”,initialEnd)
)
)
),
1.
0
)
)
);
BasicDBObject是第一个参数,1和0是第二个和第三个参数,Morphia wrapper将正确解释它们
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
)
)
);