如何使MongoCollectoin返回java.sql.Timestamp?

时间:2015-10-29 16:36:26

标签: java mongodb mongodb-java

基于https://jira.mongodb.org/browse/JAVA-1741我正在尝试实现和使用TimestampCodec,并期望在读取MongoCollection时使用decode(),但由于某种原因,decode()不是 执行,因此数据库中的IsoDate值转换为java.util.Date而不是预期的java.sql.Timestamp。

在检索MongoCollection时使用TimestampCodec.decode()的正确方法是什么?

我有以下内容,但它失败了 java.lang.AssertionError:expected:< class [Ljava.sql.Timestamp;>但是:< class java.util.Date>

@Test
public void testTimestamp() {
    Map<BsonType, Class<?>> replacements = new HashMap<BsonType, Class<?>>();
    replacements.put(BsonType.DATE_TIME, Timestamp.class);
    BsonTypeClassMap bsonTypeClassMap = new BsonTypeClassMap(replacements);
    DocumentCodecProvider documentCodecProvider =
            new DocumentCodecProvider(bsonTypeClassMap);
    CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
    CodecRegistries.fromCodecs(
            new TimestampCodec()),
            MongoClient.getDefaultCodecRegistry(),
            CodecRegistries.fromProviders(documentCodecProvider));
    Builder optionsBuilder = new MongoClientOptions.Builder();
    optionsBuilder.codecRegistry(MongoCodecs.codecRegistry());
    MongoClientOptions options = optionsBuilder.build();
    MongoClient mongo = new MongoClient(new ServerAddress(), options);
    MongoDatabase db = mongo.getDatabase("mydb");
    MongoCollection<Document> collection = db.getCollection("test");
    try {
        collection.insertOne(new Document("date", new Timestamp(new Date().getTime())));
        assertEquals(Timestamp[].class, collection.find().first().get("date").getClass());
    } finally {
        collection.drop();
        mongo.close();
    }
}

1 个答案:

答案 0 :(得分:3)

除了轻微的错误,解决方案是改变编解码器注册表的顺序,以便MongoClient.getDefaultCodecRegistry()是最后一个。

正确的代码是:

@Test
public void testTimestamp() {
    Map<BsonType, Class<?>> replacements = new HashMap<BsonType, Class<?>>();
    replacements.put(BsonType.DATE_TIME, Timestamp.class);
    BsonTypeClassMap bsonTypeClassMap = new BsonTypeClassMap(replacements);
    DocumentCodecProvider documentCodecProvider =
            new DocumentCodecProvider(bsonTypeClassMap);
    CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
            CodecRegistries.fromCodecs(
                    new TimestampCodec()),
            CodecRegistries.fromProviders(documentCodecProvider),
            MongoClient.getDefaultCodecRegistry());
    Builder optionsBuilder = new MongoClientOptions.Builder();
    optionsBuilder.codecRegistry(codecRegistry);
    MongoClientOptions options = optionsBuilder.build();
    MongoClient mongo = new MongoClient(new ServerAddress(), options);
    MongoDatabase db = mongo.getDatabase("mydb");
    MongoCollection<Document> collection = db.getCollection("test");
    try {
        collection.insertOne(new Document("date", new Timestamp(new Date().getTime())));
        assertEquals(Timestamp.class, collection.find().first().get("date").getClass());
    } finally {
        collection.drop();
        mongo.close();
    }
}