title: MongoDB getCollection with a specific document class
body: <p>My problem is to cast <code>MongoDB</code> document in one type : </p>
  
  <p>The <code>getCollection</code> <code>javadoc</code> says :</p>
  
  <pre><code>  /**
       * Gets a collection, with a specific default document class.
       *
       * @param collectionName the name of the collection to return
       * @param documentClass  the default class to cast any documents returned from the database into.
       * @param &lt;TDocument&gt;    the type of the class to use instead of {@code Document}.
       * @return the collection
       */
      &lt;TDocument&gt; MongoCollection&lt;TDocument&gt; getCollection(String collectionName, Class&lt;TDocument&gt; documentClass);
  </code></pre>
  
  <p>Here my implementation : </p>
  
  <pre><code>public class AccountDAO {
  
      public static FindIterable&lt;AccountDTO&gt; accountPersist(AccountDTO accountDTO){
          MongoCollection&lt;AccountDTO&gt; accountDataCollection = Utils.getDbCollection();
          accountDataCollection.insertOne(accountDTO);
          return accountDataCollection.find();
      }
  }
  </code></pre>
  
  <p>and the <code>getDbCollection</code> : </p>
  
  <pre><code>public static MongoCollection&lt;AccountDTO&gt;  getDbCollection() {
          MongoDatabase db = Utils.getDbConnect();
          MongoCollection&lt;AccountDTO&gt; accountDataCollection = null ;
          accountDataCollection = db.getCollection(AccountDTO.COLLECTION_NAME,AccountDTO.class);
          return accountDataCollection;
      }
  </code></pre>
  
  <p>CodecImplementationProvider : </p>
  
  <pre><code>package utils;
  
  import org.bson.BsonReader;
  import org.bson.BsonWriter;
  import org.bson.codecs.Codec;
  import org.bson.codecs.DecoderContext;
  import org.bson.codecs.EncoderContext;
  import org.bson.codecs.configuration.CodecRegistry;
  
  import digester.Account;
  import digester.Customer;
  import dto.AccountDTO;
  
  // the Codec extends two interfaces: Encoder&lt;T&gt;, Decoder&lt;T&gt;
  class MyAccountDTOImpCodec implements Codec&lt;AccountDTO&gt; {
  
      private CodecRegistry codecRegistry;
  
      public MyAccountDTOImpCodec(CodecRegistry codecRegistry) {
          this.codecRegistry = codecRegistry;
      }
  
      public void encode(BsonWriter writer, AccountDTO doc, EncoderContext encoderContext) {
          writer.writeStartDocument();
          writer.writeName("IBAN");
          writer.writeString(doc.getIBAN());
          writer.writeName("customerFirstName");
          writer.writeString(doc.getCustomerFirstName());
          writer.writeName("customerLastName");
          writer.writeString(doc.getCustomerLastName());
          writer.writeEndDocument();
      }
  
      public Class&lt;AccountDTO&gt; getEncoderClass() {
          return AccountDTO.class;
      }
  
      public AccountDTO decode(BsonReader reader, DecoderContext decoderContext) {
          reader.readStartDocument();
          String iBAN = reader.readString("IBAN");
          String firstName = reader.readString("customerFirstName");
          String lastName = reader.readString("customerLastName");
          double balance = reader.readDouble("balance");
          String currency = reader.readString("currency");
          long customerId = reader.readInt64("customerId");
  
  
  
          reader.readEndDocument();
          Account account = new Account(iBAN, balance, currency);
          Customer customer = new Customer(firstName, lastName, customerId, account);
          AccountDTO doc = new AccountDTO(account, customer);
          return doc;
      }
  
  }
  </code></pre>
  
  <p>CodecProvider : </p>
  
  <pre><code>package utils;
  
  import org.bson.codecs.Codec;
  import org.bson.codecs.configuration.CodecProvider;
  import org.bson.codecs.configuration.CodecRegistry;
  
  import dto.AccountDTO;
  
  class MyCodecProvider implements CodecProvider {    
      public &lt;T&gt; Codec&lt;T&gt; get(Class&lt;T&gt; myClass, CodecRegistry codecRegistry) {
          if (myClass== AccountDTO.class) {
              return (Codec&lt;T&gt;) new MyAccountDTOImpCodec(codecRegistry);
          }
          return null;
      }
  }
  </code></pre>
  
  <p>When running my test with <code>JUnit</code> I got this stack : </p>
  
  <pre><code>org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class dto.AccountDTO.
  </code></pre>
  
  <p>I think it's a bad implementation, in <code>Node.js</code> I would have send my <code>json</code> object, seems weird to send a <code>java</code> object to <code>MongoDB</code>.</p>
  
  <p>Is it just a bad interpretation of <code>javadoc</code> or kind of lack in my code ?</p>