简体   繁体   English

Morphia java.util.Arrays$ArrayList 在进行 Projection.projection 时无法转换为 com.mongodb.DBObject

[英]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.

相关问题 java.lang.ClassCastException:org.bson.types.ObjectId无法转换为com.mongodb.DBObject - java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject com.mongodb.DBObject / Java的BSON序列化器 - com.mongodb.DBObject / BSON serializer for java java.lang.ClassCastException: java.util.Arrays$ArrayList 不能转换为 java.util.ArrayList - java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList 为什么会出现此异常 - java.lang.ClassCastException:java.lang.String无法强制转换为com.mongodb.DBObject - Why has this exception started occurring - java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject 代码可以运行,但是给我java.util.Arrays $ ArrayList无法转换为java.util.ArrayList - Code runs but gives me java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList java.util.Arrays$ArrayList 不能转换为 java.util.LinkedHashSet - java.util.Arrays$ArrayList cannot be cast to java.util.LinkedHashSet java.lang.ClassCastException:java.util.Arrays $ ArrayList无法强制转换为java.lang.Integer - java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer java.lang.ClassCastException: java.util.Arrays$ArrayList 不能在 DAO 中转换为 java.lang.Integer - java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer in DAO 转换Arrays.asList导致异常:java.util.Arrays $ ArrayList无法强制转换为java.util.ArrayList - casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList 受休眠限制,无法将java.util.Arrays $ ArrayList强制转换为java.lang.String - java.util.Arrays$ArrayList cannot be cast to java.lang.String on hibernate restriction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM