Is there a way to access a value of a JSON document from mongodb which is inside an ArrayList?

Viewed 43

I have a code snippet that looks like this

MongoDatabase db = DbConnection.getDbConnection(); // connection is successfully made
MongoCollection collection = db.getCollection("candidates"); collection called candidates in the database

ArrayList<String> newList = new ArrayList<>();

FindIterable<Document> findIterable =  collection.find()
    .projection(fields(include("CandidateId"), excludeId()));

for(Document document:findIterable) {
  newList.add(String.valueOf(document));            
  }
System.out.println(newList);

The output looks like this

[Document{{CandidateId=1}}, Document{{CandidateId=2}}]

Is there any way I could get an arrayList that looks like this, where i only need the value of the CandidateId field

[1,2]
1 Answers
Related