英文:
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
{
"_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 . 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<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)))));
答案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<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 https://stackoverflow.com/questions/31643109/mongodb-aggregation-with-java-driver for detailled answer per driver version
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论