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