[英]Mongojack: invalid hexadecimal representation of an ObjectId
我正在尝试使用mongojack将一些数据推送到 mongo db。 我希望结果在数据库中是这样的:
{
"_id": "840617013772681266",
"messageCount": 69420,
"seedCount": 18,
"prefix": "f!",
"language": "en"
}
相反,我在控制台中收到此错误。
Caused by: java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [840617013772681266]
at org.bson.types.ObjectId.parseHexString(ObjectId.java:390)
at org.bson.types.ObjectId.<init>(ObjectId.java:193)
at org.mongojack.internal.ObjectIdSerializer.serialiseObject(ObjectIdSerializer.java:66)
at org.mongojack.internal.ObjectIdSerializer.serialize(ObjectIdSerializer.java:49)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
... 59 more
这是我尝试在数据库中创建新公会时调用的代码:
public static Guild getGuild(String id) throws ExecutionException {
return cache.get(id);
}
cache
如下(加载执行):
private static LoadingCache<String, Guild> cache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(
new CacheLoader<>() {
@Override
public Guild load(@NotNull String id) {
return findGuild(id).orElseGet(() -> new Guild(id, "f!"));
}
});
首先调用的findGuild
方法:
public static Optional<Guild> findGuild(String id) {
return Optional.ofNullable(guildCollection.find()
.filter(Filters.eq("_id", id)).first());
}
最后是Guild
文件。
@Getter
@Setter
public class Guild implements Model {
public Guild(String id, String prefix) {
this.id = id;
this.prefix = prefix;
}
public Guild() {
}
private String id;
/*
If a Discord guild sent 1,000,000,000 messages per second,
it would take roughly 292471 years to reach the long primitive limit.
*/
private long messageCount;
private long seedCount;
// The default language is specified in BotValues.java's bot.yaml.
private String language;
private String prefix;
@ObjectId
@JsonProperty("_id")
public String getId() {
return id;
}
@ObjectId
@JsonProperty("_id")
public void setId(String id) {
this.id = id;
}
}
我尝试了多种方法,例如做Long.toHexString(Long.parseLong(id))
事实是我不完全理解错误,在查看文档后,我留下的问题多于答案。
ObjectId 是一个 12 字节的值,通常表示为 24 个十六进制数字的序列。 它不是 integer。
您可以使用适当的 ObjectId 构造函数创建 ObjectId 值,也可以解析 24 位十六进制数字字符串。 您似乎正在尝试执行 integer 到 ObjectId 的转换,这通常不是受支持的操作。
You can technically convert the integer 840617013772681266
to an ObjectId by zero-padding it to 12 bytes, but standard MongoDB driver tooling doesn't do that for you and considers this invalid input (either as an integer or as a string) for conversion to ObjectId .
Ruby 中的示例:
irb(main):011:0> (v = '%x' % 840617013772681266) + '0' * (24 - v.length)
=> "baa78b862120032000000000"
请注意,虽然结果值可以解析为 ObjectId,但它不是按照 ObjectId 规则构造的,因此无法将值合理地分解为 ObjectId 组件(机器 ID、计数器和随机值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.