2

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" : "[email protected]", 
    "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)))));

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried AggregateIterable also it is giving java.lang.ClassCastException: org.bson.Document cannot be cast to java.util.List AggregateIterable<Document> user1 =users.aggregate((List<? extends Bson>) new Document("$project", new Document("Claim Systems", new Document("$match", new Document("Claim Systems.AtlasSystem",searchString)))));
Is there anything wrong with Java synatx , anyother way to get the result value
You added an other false cast, try to avoid any, e.g.: AggregateIterable doc = users.aggregate(Collections.singletonList(...));
I tried below syntax AggregateIterable<Document> user1 =users.aggregate((List<? extends Bson>) new Document("$project", new Document("Claim Systems", new Document("$match", new Document("Claim Systems.AtlasSystem",searchString))))); .It is aslo giving same exception
1

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 MongoDB aggregation with Java driver for detailled answer per driver version

8 Comments

Thanks for your suggestion @Issam ,I have tried as suggested but getting below exception .' Unrecognized expression '$match
Welcome. I see that you have edited your question after i posted my answer. Is it working for you now?
Still facing an issue.Getting an exception casting exception java.lang.ClassCastException: org.bson.Document cannot be cast to java.util.List .One more Query I have ,Do we need to add Documents if we want to get the 'N' of Systems value
You are trying to cast a Document to List with this (List<? extends Bson>) hence the error. Have a look here stackoverflow.com/questions/31643109/… it might help.
Thanks a lot for sharing the link .It helped a lot .And I changed the synatx as below AggregateIterable<Document> aggregateIterable = users.aggregate(Arrays.asList(new Document("$project", new Document("Claim Systems", new Document("$match", new Document("Claim Systems.Atlas", searchString)))))); Iterator iterator = aggregateIterable.iterator(); ArrayList<Document> documents = new ArrayList(); // Then iterate over the iterator while (iterator.hasNext()) { documents.add((Document) iterator.next()); }/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.