简体   繁体   English

Mongojack:ObjectId 的无效十六进制表示

[英]Mongojack: invalid hexadecimal representation of an ObjectId

Goal目标

I am trying to push some data to a mongo db using mongojack .我正在尝试使用mongojack将一些数据推送到 mongo db。 I expect the result to be something like this in the db:我希望结果在数据库中是这样的:

{
 "_id": "840617013772681266",
 "messageCount": 69420,
 "seedCount": 18,
 "prefix": "f!",
 "language": "en"
}

Problem问题

Instead, I get this error in my console.相反,我在控制台中收到此错误。

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

Code代码

This is the code that gets called when I try to create a new Guild in the db:这是我尝试在数据库中创建新公会时调用的代码:

    public static Guild getGuild(String id) throws ExecutionException {
        return cache.get(id);
    }

cache is the following (load get executed): 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!"));
                        }
                    });

The findGuild method that gets called first:首先调用的findGuild方法:

    public static Optional<Guild> findGuild(String id) {
        return Optional.ofNullable(guildCollection.find()
                .filter(Filters.eq("_id", id)).first());
    }

And finally the Guild document.最后是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;
    }
}

What I've tried我试过的

I've tried multiple things, such as doing Long.toHexString(Long.parseLong(id)) truth is I don't understand the error completely and after seeing documentation I'm left with more questions than answers.我尝试了多种方法,例如做Long.toHexString(Long.parseLong(id))事实是我不完全理解错误,在查看文档后,我留下的问题多于答案。

ObjectId is a 12-byte value that is commonly represented as a sequence of 24 hex digits. ObjectId 是一个 12 字节的值,通常表示为 24 个十六进制数字的序列。 It is not an integer.它不是 integer。

You can either create ObjectId values using the appropriate ObjectId constructor or parse a 24-hex-digit string.您可以使用适当的 ObjectId 构造函数创建 ObjectId 值,也可以解析 24 位十六进制数字字符串。 You appear to be trying to perform an integer conversion to ObjectId which generally isn't a supported operation.您似乎正在尝试执行 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. 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 .

Example in Ruby: Ruby 中的示例:

irb(main):011:0> (v = '%x' % 840617013772681266) + '0' * (24 - v.length)
=> "baa78b862120032000000000"

Note that while the resulting value would be parseable as an ObjectId, it isn't constructed following the ObjectId rules and thus the value cannot be sensibly decomposed into the ObjectId components (machine id, counter and a random value).请注意,虽然结果值可以解析为 ObjectId,但它不是按照 ObjectId 规则构造的,因此无法将值合理地分解为 ObjectId 组件(机器 ID、计数器和随机值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM