1

Quick and hopefully easy question.

DBObject res = collection.findOne(new BasicDBObject("_id",id));
// some_array is just an array of strings
Arr = res.get("some_array");

What type does Arr need to be? String[]? ArrayList?

2 Answers 2

1

res.get() returns an Object, so that you need to do an explicit casting (you need to know the type of some_array field.

i.e:

List<Integer> values = (List<Integer>)res.get("some_array");

This is one of the reasons because I'm not supporter of bringing Java & MongoDB together.

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

Comments

0

The Java driver will return arrays within the document as a List - for your example, you would want Arr to be a List<String>.

1 Comment

No at all. The Java driver returns Objects, not Arrays/Lists. Furthermore, by casting some_array to List<String> could trigger a ClassCastException if types mismatch.

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.