Summary
TreeSet can be properly encoded into the database but cannot be decoded when being used in a POJO. This is because CollectionPropertyCodecProvider only accounts for HashSet and not for TreeSet.
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
Running with version 4.11.1 against a standalone (but I presume the issue can be reproduced with any cluster topology).
How to Reproduce
public class TreeSetDecodingBugPoc { public static void main(String... args) { CodecRegistry registry = fromRegistries( MongoClientSettings.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build())); MongoClient client = MongoClients.create(); Example example = new Example(new TreeSet<>(List.of("one", "two"))); client.getDatabase("exampleDb").getCollection("exampleColl", Example.class).withCodecRegistry(registry).insertOne(example); Example insertedExample = client.getDatabase("exampleDb").getCollection("exampleColl", Example.class).withCodecRegistry(registry).find().first(); System.out.println(insertedExample.getExample()); } public static class Example { @BsonProperty("example") public SortedSet<String> example = new TreeSet<>(); @BsonCreator public Example(@BsonProperty("example") TreeSet<String> example) { this.example = example; } @BsonProperty("example") public SortedSet<String> getExample() { return this.example; } @BsonProperty("example") public void setExample(SortedSet<String> example) { this.example = example; } } }
The document is stored in the DB properly (queried using mongosh):
{ _id: ObjectId('65c05203c9d2486e182f3e65'), example: [ 'one', 'two' ] }
Querying for it will yield the exception:
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'Example'. Decoding 'example' errored with: Unsupported Collection interface of java.util.SortedSet!