Java 有没有办法从ArrayList中的mongodb访问JSON文档的值?
我有一个代码片段,看起来像这样Java 有没有办法从ArrayList中的mongodb访问JSON文档的值?,java,json,mongodb,arraylist,Java,Json,Mongodb,Arraylist,我有一个代码片段,看起来像这样 MongoDatabase db = DbConnection.getDbConnection(); // connection is successfully made MongoCollection collection = db.getCollection("candidates"); collection called candidates in the database ArrayList<String> newList
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);
[Document{{CandidateId=1}}, Document{{CandidateId=2}}]
有没有办法得到这样的arrayList,我只需要CandidateId字段的值
[1,2]
您可以这样使用:
newList.add(document.getString("CandidateId"));