[英]Retrieving an array of Documents from mongoDB with Java
我有一個包含員工文件的集合。 每個員工都有一個包含該員工當前參與的項目的文檔數組。問題是我不知道如何獲取/查詢項目數組?
這是此類文檔的示例。
{name : "employe name" , age : "..employe age" , phoneNr : 12334 , projects :
[{ projectName : "project name" , projectLeder : "project leader" }] }
這是用於檢索文檔中除項目數組之外的值的示例代碼。
public void printEmployees() {
MongoCursor<Document> cursor = coll.find().iterator();
while(cursor.hasNext()) {
Document documentEmployee = cursor.next();
System.out.println((String) documentEmployee.get("name"));
System.out.println((Integer) documentEmployee.get("age"));
System.out.println((Integer) documentEmployee.get("phoneNr"));
//How do i query/extract the project array?
}
}
你可以嘗試這樣的事情。
while(cursor.hasNext()) {
Document documentEmployee = cursor.next();
System.out.println(documentEmployee.getString("name"));
System.out.println(documentEmployee.getInteger("age"));
System.out.println(documentEmployee.getInteger("phoneNr"));
//Extract
ArrayList projects = documentEmployee.get("project", ArrayList.class);
//Map
for (Object obj : projects) {
Document project = (Document) obj;
project.getString("projectName");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.