title: Can't find a codec for my class
body: <p>I have simple class named Signal. Class looks as follows:</p>
  
  <pre><code>public class Signal {
      private String id;
      private Date timestamp;
  
      public String getId() {
          return id;
      }
      public void setId(String id) {
          this.id = id;
      }
      public Date getTimestamp() {
          return timestamp;
      }
      public void setTimestamp(Date timestamp) {
          this.timestamp = timestamp;
      }
  }
  </code></pre>
  
  <p>I am trying to insert signal in MongoDB (v3.4). I am using the following method to insert:</p>
  
  <pre><code>public boolean xyz(Signal signal) {
              try {
                  DatabaseConnection databaseConnection =DatabaseConnection.getInstance();
                  MongoClient mongoClient = databaseConnection.getMongoClient();
                  MongoDatabase db = mongoClient.getDatabase("myDb"); 
                  MongoCollection&lt;Signal&gt; collection = db.getCollection("myCollection", Signal.class);
                  collection.insertOne(signal);
  
                  return true;
              } catch (Exception e){
                  logger.error("Error", e);
                  return false;
              }
  
          }
  </code></pre>
  
  <p>I am getting the following exception:</p>
  
  <blockquote>
    <p>org.bson.codecs.configuration.CodecConfigurationException: Can't find
    a codec for class in.co.mysite.webapi.models.Signal.</p>
  </blockquote>
  
  <p>I checked a similar question <a href="https://stackoverflow.com/questions/39433775/mongodb-java-inserting-throws-org-bson-codecs-configuration-codecconfigurationex">here</a> but insertion code is different. I took the hint from answer and modified my method but it doesn't look clean. Modified method is as follows: </p>
  
  <pre><code>public boolean xyz(Signal signal) {
          try {
              DatabaseConnection databaseConnection =DatabaseConnection.getInstance();
              MongoClient mongoClient = databaseConnection.getMongoClient();
              MongoDatabase db = mongoClient.getDatabase("myDb"); 
              MongoCollection&lt;Document&gt; collection = db.getCollection("myCollection");
  
              Document doc = new Document();
  
              doc.put("id", signal.getId());
              doc.put("timestamp", signal.getTimestamp());
              doc.put("_id", new ObjectId().toString());
  
              collection.insertOne(doc);
  
              return true;
          } catch (Exception e){
              logger.error("Error", e);
              return false;
          }
  
      }
  </code></pre>