title: Call a Mongo function from aggregation pipeline through JAVA Driver API
body: <p>I am able to get the following working in mongosh</p>
  <pre><code>var pipeline = [
      { $group: { _id: &quot;$column1&quot;, valArray: { $push: &quot;$column2&quot; } } },
      { $set: {
              val: function1(&quot;$valArray&quot;, 70)    
          }
      },
      { $unset: [ &quot;valArray&quot; ] },
      { $sort: { _id: 1 } }
  ];
  </code></pre>
  <p>function1 is function defined to take an array and a number as param</p>
  <p>When I try the same with Java I get the error while trying to create List</p>
  <pre><code>List&lt;Bson&gt; pipeline = Stream.of(new String[] {
      &quot;{ $group: { _id: \&quot;$column1\&quot;, valArray: { $push: \&quot;$column2\&quot; } } }&quot;,
      &quot;{ $set: { percentile_n: function1(\&quot;$valArray\&quot;, &quot; + percentileN + &quot;) } }&quot;,
      &quot;{ $unset: [ \&quot;valArray\&quot; ] }&quot;,
      &quot;{ $sort: { _id: 1 } }&quot;
    })
    .map(s -&gt; Document.parse(s).toBsonDocument())
    .collect(Collectors.toList());
  
  
  Exception in thread &quot;main&quot; org.bson.json.JsonParseException: **JSON reader was expecting a value but found 'function1'**.
      at org.bson.json.JsonReader.readBsonType(JsonReader.java:263)
      at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:175)
      at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:44)
      at org.bson.internal.LazyCodec.decode(LazyCodec.java:48)
      at org.bson.codecs.ContainerCodecHelper.readValue(ContainerCodecHelper.java:61)
      at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:177)
      at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:44)
      at org.bson.Document.parse(Document.java:126)
      at org.bson.Document.parse(Document.java:111)
  </code></pre>
  <p>Looks like there is a specific way to create BsonDocument for a function call. Can anyone please help?</p>