1

I have a collection containing documents of employees. Each employee have an array of documents with the projects this employee is currently involved in. The problem is that i don't know how to get/query the array of projects?

Here is an example of such document.

{name : "employe name" , age : "..employe age" , phoneNr : 12334 , projects : 
[{ projectName : "project name" , projectLeder : "project leader" }] }

Here is an example code for retrieving the values in the document except the project array.

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?

        }  

  }    

1 Answer 1

1

You can try something like this.

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");
   }
}
Sign up to request clarification or add additional context in comments.

Comments

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.