1

I have an array in Mongo that I am trying to throw into a list.

Here is what I have

BasicDBList computerList = new BasicDBList(); 
        if (dbObj.get("computers") != null){
            computerList =  (BasicDBList) dbObj.get("computers");
        }
        for (Object obj : computerList) { 
            System.getComputers().add((Computer) obj);
        }

I have a main System object that has a List in it and that is what I am doing here is trying to retrieve computers out of Mongo and throw back into the dataObject.

I am getting the error

Caused by: java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.me.systems.commons.entities.Computer

Any ideas on what I can do?

EDIT: I did obfuscate the names of the objects, but I double checked and they are consistent and accurate to the syntax.

4
  • 1
    Is Data the correct data type? Is com.me.systems.commons.entities.Data the correct path to the type? Commented May 22, 2013 at 18:57
  • Yup, maybe "Data" was a bad obfuscated name for it. I am going to change it so it isn't confusing. EDIT: I changed "Data" to Computer. Commented May 22, 2013 at 19:07
  • clearly the error says computerList is a List<BasicDBObject> not a List<Computer>. If 'dbObj' is directly retrieved from db without using any ORM, then it will be List<BasicDBObject>, hence the error. Commented May 23, 2013 at 1:33
  • That doesn't make any sense. computerList is a BasicDBList which should handle what you are talking about? Commented May 23, 2013 at 12:04

1 Answer 1

2

I'm assuming that your variable dbObj is a DBObject that has been retrieved from MongoDB, and I'm also assuming the ClassCastException is thrown on line 6 of your example code.

The problem is that you're casting on line 6 in your code sample, and you're casting to the wrong type. When you get something back from MongoDB, you don't get your Java objects back (i.e. Computer). Any document or sub-document in the database will come back as a BasicDBObject. And this cannot be cast to Computer.

What you need to do is take the relevant information out of the BasicDBObject and then create a new Computer out of it. So, say for example your Computer looks like this:

class Computer {
    String name;
    int ipAddress;

    Computer(final String name, final int ipAddress) {
        this.name = name;
        this.ipAddress = ipAddress;
    }
}

then you will probably want to do something like:

final BasicDBObject dbObj = null;
if (dbObj.get("computers") != null){
    computerList =  (BasicDBList) dbObj.get("computers");
}
for ( obj : computerList) {
    final BasicDBObject computerAsDBObject = (BasicDBObject) obj;
    final Computer computer = new Computer(computerAsDBObject.getString("name"), 
                                           computerAsDBObject.getInt("ipAddress"));
    System.getComputers().add(computer);
}

When you're working with the Java driver directly, you will only get a very limited set of types back from the database - i.e. Strings, ints, longs and other primitives, and simple objects like BasicDBList and BasicDBObject.

What Abhishek was alluding to in the comment was that if you use an ODM like Morphia or Spring Data, you will get "real" Java objects like your Computer class back from Mongo, but you won't get that with the driver alone.

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

Comments

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.