[英]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.