简体   繁体   English

java.lang.ClassCastException:com.mongodb.client.internal.AggregateIterableImpl 无法转换为 java.util.ArrayList

[英]java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl cannot be cast to java.util.ArrayList

I have got below document.我有以下文件。 I need to match with the requestParam System Bob with the below collection and need to get the result value if requestParam matches with the Email Systems.Bob.System.我需要将 requestParam System Bob 与以下集合匹配,并且如果 requestParam 与 Email Systems.Bob.System 匹配,则需要获取结果值。

Here RequestParam is system这里 RequestParam 是系统

{ 
    "_id" : ObjectId("5f0890e870e631865877e"), 
    "user" : "testuser", 
    "Email" : "testuser@sample.com", 
    "Batch Systems" : [
        "STAR", 
        "STORY", 
        "ITEMS",    
    ], 
    "Email Systems" : [
        {
            "Bob" : {
                "System" : "Bob", 
                **"result"** : true
            }
        }, 
        {
            "Wild" : {
                "System" : "Wild", 
                "result" : true
            }
        },
        {
            "CRaft" : {
                "System" : "Craft", 
                "result" : false
            }
        }
    ]
}

I have tried with the below syntax, getting the java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl cannot be cast to java.util.ArrayList.我尝试使用以下语法,得到 java.lang.ClassCastException:com.mongodb.client.internal.AggregateIterableImpl cannot be cast to java.util.ArrayList。 Can anyone tell me what is wrong with the below code and help me with the synatx.谁能告诉我下面的代码有什么问题并帮助我解决 synatx.

MongoDatabase database = this.mongoClient.getDatabase(this.database);
    MongoCollection<Document> user = database.getCollection(COLLECTION);
    Document userQuery = new Document();
    String searchString = new String(system);
    AggregateIterable<Document> user1 =users.aggregate((List<? extends Bson>) new Document("$project",
                new Document("Email Systems",
                        new Document("$match",
                                new Document("Email  Systems.BobSystem",searchString)))));

The object returned by users.aggregate() is an AggregateIterable. users.aggregate() 返回的 object 是一个 AggregateIterable。 ArrayList does not implement this interface so your cast fails. ArrayList 未实现此接口,因此您的转换失败。 Try to cast and work with it as an AggregateIterable or AggregateIterableImpl.尝试将其作为 AggregateIterable 或 AggregateIterableImpl 进行转换和使用。

users.aggregate() return an AggregateIterable and not an ArrayList hence the error. users.aggregate()返回一个AggregateIterable而不是ArrayList因此错误。 You can create an Iterator from AggregateIterable and then add documents to an ArrayList .您可以从AggregateIterable创建Iterator ,然后将文档添加到ArrayList

MongoDatabase database = this.mongoClient.getDatabase(this.database);
MongoCollection<Document> user = database.getCollection(COLLECTION);
Document userQuery = new Document();
String searchString = new String(system);
AggregateIterable aggregateIterable = users.aggregate(Collections.singletonList(new Document("$project", new Document("Email Systems", new Document("$match", new Document("Email Systems.Bob.System", searchString))))));
// Create an iterator from the iterable
Iterator iterator = aggregateIterable.iterator();
ArrayList<Document> documents = new ArrayList();
// Then iterate over the iterator
while (iterator.hasNext()) {
    documents.add((Document) iterator.next());
}

See MongoDB aggregation with Java driver for detailled answer per driver version有关每个驱动程序版本的详细答案,请参阅MongoDB 聚合与 Java 驱动程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:无法将java.util.ArrayList强制转换为com.inrev.crm.bean.IRSurveyQuestions - java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.inrev.crm.bean.IRSurveyQuestions java.lang.ClassCastException: java.util.ArrayList 不能转换为 com.parse.ParseObject - java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.parse.ParseObject 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.util.ArrayList无法强制转换为java.lang.String - java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String 如何修复:java.lang.ClassCastException:java.util.ArrayList无法强制转换为java.lang.Integer - How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer java.lang.ClassCastException: java.util.ArrayList 不能转换为 java.lang.Object[] - java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Object[] 处理 java.lang.ClassCastException: java.util.ArrayList 不能强制转换为 checksAccount.BankEntry - Dealing java.lang.ClassCastException: java.util.ArrayList cannot be cast to checkingAccount.BankEntry 如何修复:java.lang.ClassCastException:java.util.ArrayList无法强制转换为double [] - How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to double[] java.lang.ClassCastException:无法将java.util.ArrayList强制转换为…CrExcessMaster - java.lang.ClassCastException: java.util.ArrayList cannot be cast to …CrExcessMaster java.lang.ClassCastException:无法将java.util.ArrayList强制转换为antlr.collections.List - java.lang.ClassCastException: java.util.ArrayList cannot be cast to antlr.collections.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM