Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB Java自己的编解码器不工作_Java_Mongodb_Class_Codec_Provider - Fatal编程技术网

MongoDB Java自己的编解码器不工作

MongoDB Java自己的编解码器不工作,java,mongodb,class,codec,provider,Java,Mongodb,Class,Codec,Provider,我正在尝试使用own.class从MongoDB保存/获取文档 我创建了自己的编解码器,除了解码,其他一切都很好 我花了很多时间重新创建、搜索解决方案,但我仍然不明白它不起作用的原因:/ 试验方法: CodecRegistry codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromProviders(new UserCodecProvider()), MongoCli

我正在尝试使用own.class从MongoDB保存/获取文档 我创建了自己的编解码器,除了解码,其他一切都很好

我花了很多时间重新创建、搜索解决方案,但我仍然不明白它不起作用的原因:/

试验方法:

CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
            CodecRegistries.fromProviders(new UserCodecProvider()),
            MongoClients.getDefaultCodecRegistry());

    MongoCredential credential = MongoCredential.createCredential(user, base, pass);

    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Arrays.asList(new ServerAddress(host, port)))
            .build();

    MongoClientSettings settings = MongoClientSettings.builder()
            .clusterSettings(clusterSettings)
            .codecRegistry(codecRegistry)
            .credentialList(Arrays.asList(credential))
            .build();

    MongoClient mongoClient = MongoClients.create(settings);
    MongoDatabase database = mongoClient.getDatabase(base);
    MongoCollection<User> collection = database.getCollection("test", User.class);

    CountDownLatch countDownLatch = new CountDownLatch(1);

    User user = new User();
    user.setPoints(1);
    user.setName("Test");

    collection.insertOne(user, (aVoid, throwable) -> {
        System.out.println(user);
        countDownLatch.countDown();
    });

    countDownLatch.await();

    CountDownLatch countDownLatch2 = new CountDownLatch(1);

    collection.find(eq("name", "Test")).first((user1, throwable) -> {
        System.out.println(user1);
        countDownLatch2.countDown();
    });

    countDownLatch2.await();
User [name=Test, points=1]
null

Process finished with exit code 0
试验方法的输出:

CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
            CodecRegistries.fromProviders(new UserCodecProvider()),
            MongoClients.getDefaultCodecRegistry());

    MongoCredential credential = MongoCredential.createCredential(user, base, pass);

    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Arrays.asList(new ServerAddress(host, port)))
            .build();

    MongoClientSettings settings = MongoClientSettings.builder()
            .clusterSettings(clusterSettings)
            .codecRegistry(codecRegistry)
            .credentialList(Arrays.asList(credential))
            .build();

    MongoClient mongoClient = MongoClients.create(settings);
    MongoDatabase database = mongoClient.getDatabase(base);
    MongoCollection<User> collection = database.getCollection("test", User.class);

    CountDownLatch countDownLatch = new CountDownLatch(1);

    User user = new User();
    user.setPoints(1);
    user.setName("Test");

    collection.insertOne(user, (aVoid, throwable) -> {
        System.out.println(user);
        countDownLatch.countDown();
    });

    countDownLatch.await();

    CountDownLatch countDownLatch2 = new CountDownLatch(1);

    collection.find(eq("name", "Test")).first((user1, throwable) -> {
        System.out.println(user1);
        countDownLatch2.countDown();
    });

    countDownLatch2.await();
User [name=Test, points=1]
null

Process finished with exit code 0
//编辑 我找到了解决办法 但是如果你知道没有这个lib怎么做,回答。 谢谢!:)

User [name=Test, points=1]
null

Process finished with exit code 0