java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl no se puede convertir a java.util.ArrayList

Sep 01 2020

Tengo el siguiente documento. Necesito coincidir con requestParam System Bob con la siguiente colección y necesito obtener el valor de resultado si requestParam coincide con Email Systems.Bob.System.

Aquí RequestParam es el sistema

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

He intentado con la siguiente sintaxis, obteniendo java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl no se puede convertir en java.util.ArrayList. ¿Alguien puede decirme qué está mal con el siguiente código y ayudarme con 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)))));

Respuestas

1 Milgo Sep 01 2020 at 15:17

El objeto devuelto por users.aggregate () es un AggregateIterable. ArrayList no implementa esta interfaz, por lo que su transmisión falla. Intente convertir y trabajar con él como AggregateIterable o AggregateIterableImpl.

1 IssamEl-atif Sep 01 2020 at 15:49

users.aggregate()devuelve un AggregateIterabley no un de ArrayListahí el error. Puede crear un Iteratordesde AggregateIterabley luego agregar documentos a un 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());
}

Consulte la agregación de MongoDB con el controlador Java para obtener una respuesta detallada por versión del controlador