Java driver 4.2.3 Document.getList throws ClassCastException

XMLWordPrintableJSON

    • Type: Question
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      while using java driver 4.2.3 with server version - 5.0.12

      Document class getList method throws ClassCastExcepion below are the details.

      Document in DB:

      {

        “name”: “hr”,
        “id”: 1,
        “employees”: [
           {
             “name”: “paul”,
             “empId”: 1
           },
          {
            “name”: “nick”,
            “empId”: 2
          }
         ]
      }

      Code:

      public void getEmpList(){
              MongoCollection<Document> coll = mongoDatabase.getCollection("Department");
              try

      {             Document doc = coll.find().first();             List<Employee> employees = doc.getList("employees", Employee.class);         }

      catch(Exception e)

      {             e.printStackTrace();         }

      }

       

      public class Employee{

          private String name;
          private Integer empId;

           public String getName()

      {          return name;     }

          public void setName(String name)

      {        this.name = name;     }

         public Integer getEmpId()

      {        return empId;    }

          public void setEmpId(Integer empId)

      {        this.empId = empId;     }

      }

       

      Output:

      java.lang.ClassCastException: List element cannot be cast to com.Employee
      at org.bson.Document.constructValuesList(Document.java:383)
      at org.bson.Document.getList(Document.java:350)

            Assignee:
            Unassigned
            Reporter:
            Sreenivas Gurramkonda
            None
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: