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

huangapple go评论101阅读模式
英文:

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

问题

我已获得以下文档。我需要将其与请求参数系统 Bob 匹配,并与下面的集合进行匹配,如果请求参数与 Email Systems.Bob.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
            }
        }
    ]
}

我已尝试以下语法,但出现了 java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl cannot be cast to java.util.ArrayList。有谁可以告诉我下面的代码有什么问题,并帮我调整语法。

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)))));
英文:

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.

Here RequestParam is system

{ 
    &quot;_id&quot; : ObjectId(&quot;5f0890e870e631865877e&quot;), 
    &quot;user&quot; : &quot;testuser&quot;, 
    &quot;Email&quot; : &quot;testuser@sample.com&quot;, 
    &quot;Batch Systems&quot; : [
        &quot;STAR&quot;, 
        &quot;STORY&quot;, 
        &quot;ITEMS&quot;,    
    ], 
    &quot;Email Systems&quot; : [
        {
            &quot;Bob&quot; : {
                &quot;System&quot; : &quot;Bob&quot;, 
                **&quot;result&quot;** : true
            }
        }, 
        {
            &quot;Wild&quot; : {
                &quot;System&quot; : &quot;Wild&quot;, 
                &quot;result&quot; : true
            }
        },
        {
            &quot;CRaft&quot; : {
                &quot;System&quot; : &quot;Craft&quot;, 
                &quot;result&quot; : 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 . Can anyone tell me what is wrong with the below code and help me with the synatx .

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

答案1

得分: 1

用户.aggregate()返回的对象是AggregateIterable。ArrayList没有实现这个接口,所以你的转换失败了。尝试将其转换并将其视为AggregateIterable或AggregateIterableImpl来处理。

英文:

The object returned by users.aggregate() is an AggregateIterable. ArrayList does not implement this interface so your cast fails. Try to cast and work with it as an AggregateIterable or AggregateIterableImpl.

答案2

得分: 1

users.aggregate()方法返回一个AggregateIterable,而不是一个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))))));
// 从可迭代对象创建迭代器
Iterator iterator = aggregateIterable.iterator();
ArrayList<Document> documents = new ArrayList();
// 然后遍历迭代器
while (iterator.hasNext()) {
    documents.add((Document) iterator.next());
}

更多细节可以参考这个链接:https://stackoverflow.com/questions/31643109/mongodb-aggregation-with-java-driver,根据驱动程序版本提供了详细的答案。

英文:

users.aggregate() return an AggregateIterable and not an ArrayList hence the error.
You can create an Iterator from AggregateIterable and then add documents to an ArrayList.

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

See https://stackoverflow.com/questions/31643109/mongodb-aggregation-with-java-driver for detailled answer per driver version

huangapple
  • 本文由 发表于 2020年9月1日 15:06:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63682911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定