gpt4 book ai didi

java - 尝试插入 java 枚举时出现 mongodb 编解码器注册表错误

转载 作者:可可西里 更新时间:2023-11-01 10:48:00 26 4
gpt4 key购买 nike

我有一个名为 Person.class 的类,其中有一个名为 sex 的枚举。

public enum Sex{
Male,Female
}

我有一个变量来放置这个枚举值。

private Sex sex = Sex.Male;

public Sex getSex() {
return sex;
}

public void setSex(Sex sex) {
this.sex = sex;
}

它正在插入,但是当我尝试更新或检索它时,它抛出了这个名为

的错误
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class in.co.precisionit.pivot.database.model.Person$Sex.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)

更新查询:

collection.updateOne(eq("employeeList.user", employeeReference),set("user.$.sex", sex)).getModifiedCount();

检索查询:

Aggregates.unwind("$user");
Aggregates.match(eq("user.sex", sex));
Aggregates.group("$_id", Accumulators.push("user", "$user"));
collection.aggregate(Arrays.asList(unwind, match, group)).first().getUser();

请帮忙。

最佳答案

我今天也遇到了这个问题。这似乎是 3.6.0 驱动程序中的错误。有一个新版本(3.6.1),似乎没有这个问题。

关于java - 尝试插入 java 枚举时出现 mongodb 编解码器注册表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com