0

I am saving a BasicDBObject in my mongoDb collection

BasicDBObject[] aBox = new BasicDBObject[5];
BasicDBObject obj = new BasicDBObject();
obj.append("box", aBox);

for the moment this is a void array so I have just [] in my collection

but when I am trying to get my array

DBObject[] aBox = (DBObject[]) obj.get("box");

I have this error

java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to [Lcom.mongodb.DBObject;

Do you know how to have my array?

1 Answer 1

1

BasicBDList is a List and therefore has a toArray method. Or you could just keep it as a list. Something like this should work:

List<BasicDBObject> list = (List<BasicDBObject>) obj.get("box");
BasicDBObject[] aBox = list.toArray(new BasicDBObject[list.size()]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help, s I can also directly save list I have not to convert to array before saving

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.