title: How to do aggregate query with accumulators using mongodb driver in java
body: <p>I'm quite new in MongoDB and its interaction with java.
  I'm using this driver</p>
  
  <pre><code>&lt;dependency&gt;
  &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
  &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
  &lt;version&gt;3.4.2&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>and I want to perform this aggregation query:</p>
  
  <pre><code>db.getCollection('COLLECTION').aggregate(
  [
    {"$match":{"val.elem.0001":{"$exists":true}}},
    {"$project":{"FIELD_PATH":"$val.elem.0001"}},
    {$group:{_id:{"FIELD":{"$literal":"0001"}},
    "PATH":{"$addToSet":"$FIELD_PATH"}}}
  ]                                          
  );
  </code></pre>
  
  <p>The java code I've written is the following (however I'm not sure if I use correctly the addToSet method):</p>
  
  <pre><code>AggregateIterable&lt;Document&gt; output = collection.aggregate(Arrays.asList(
  new Document("$match", new Document("val.elem.0001",new Document("$exists",true))),
  new Document("$project", new Document("FIELD_PATH","$val.elem.0001")),
  new Document("$group", new Document("_id",new Document("FIELD", new Document("$literal", "0001"))
      .append("PATH",  Accumulators.addToSet("$PATH", "$FIELD_PATH"))))));
  </code></pre>
  
  <p>It is correct? Because I can't print the result on screen if I add the "append" part. The error that returns is 
  <em>Can't find a codec for class com.mongodb.client.model.BsonField</em></p>
  
  <p>So, to resume and make more readable and comprehensive what I've asked:</p>
  
  <ul>
  <li>Is the java representation of the query correct?</li>
  <li>If so, why I'm not able to print or access the result of the query?</li>
  </ul>
  
  <p>Thanks in advance, if you need more informations, I'm ready to provide them.</p>