Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-254

Converting arrays of embedded documents to custom objects

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 3.0.0
    • 2.4
    • API
    • None

    Description

      DBCollection.setObjectClass() and DBCollection.setInternalClass() offer a convinient way to convert DBObjects to custom objects without using reflection.

      However there is no proper way of converting DBObjects contained in arrays to custom objects. This is due to the way the driver retrieves "internal classes" by path.

      For instance, having:

      public class Question implements DBObject {
      /* ... */
      public List<Answer> answers;
      }

      public class Answer implements DBObject {
      /* ... */
      }

      The way to convert "answers" array items to Answer instances is as follows:

      collection.setInternalClass("answers.0", Answer.class);
      collection.setInternalClass("answers.1", Answer.class);
      collection.setInternalClass("answers.2", Answer.class);
      and so on...

      it would be nice to have a way of setting a custom class for all items inside an array? Maybe using setInternalClass or a new method:

      collection.setInternalClass("answers.$", Answer.class);
      collection.setCollectionItemClass("answers", Answer.class);

      Attachments

        1. DBCollection.java.diff
          3 kB
        2. DBCollectionTest.java.diff
          2 kB

        Activity

          People

            Unassigned Unassigned
            javierferrero Javier Ferrero
            Votes:
            6 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: