BsonValue toBson() {
      BsonDocument bson = new BsonDocument();
      org.bson.BsonWriter writer = new BsonDocumentWriter(bson);
      // Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a
      // document.
      writer.writeStartDocument();
      writer.writeName("$");
      encoder.encode(writer, value, EncoderContext.builder().build());
      writer.writeEndDocument();
      writer.flush();
      return bson.get("$");
    }
Builds bson with index definition. The difference with
{@link #convertToBson(Constraints.ConstraintHost)} is that this method fails on duplicate
fields. Currently only one index is allowed per field.
@see <a href="https://docs.mongodb.com/manual/indexes/">Mongo Indexes</a>
mongo/src/org/immutables/mongo/repository/internal/Support.java