java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl को java.util में शामिल नहीं किया जा सकता है।

Sep 01 2020

मुझे नीचे दस्तावेज मिल गए हैं। मुझे नीचे दिए गए संग्रह के साथ RequestParam System Bob के साथ मेल खाना चाहिए और यदि निवेदन पटल को Email Systems.bus.System के साथ मेल खाता है तो परिणाम मान प्राप्त करने की आवश्यकता है।

यहाँ RequestParam सिस्टम है

{ 
    "_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
            }
        }
    ]
}

मैंने java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl को java.util.Arrayerist पर नहीं डाला जा सकता है। क्या कोई मुझे बता सकता है कि नीचे दिए गए कोड में क्या गलत है और मुझे 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 Milgo Sep 01 2020 at 15:17

Users.aggregate () द्वारा दिया गया ऑब्जेक्ट एक AggregateIterable है। ArrayList इस इंटरफ़ेस को लागू नहीं करता है, इसलिए आपका कलाकार विफल रहता है। एक AggregateIterable या AggregateIterableImpl के रूप में इसके साथ कास्ट और काम करने की कोशिश करें।

1 IssamEl-atif Sep 01 2020 at 15:49

users.aggregate()एक वापसी AggregateIterableऔर ArrayListइसलिए एक त्रुटि नहीं है। आप एक Iteratorसे बना सकते हैं AggregateIterableऔर फिर एक में दस्तावेज़ जोड़ सकते हैं 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());
}

ड्राइवर संस्करण प्रति अलग जवाब के लिए जावा ड्राइवर के साथ MongoDB एकत्रीकरण देखें