简体   繁体   English

MongoDB“无效的 BSON 字段名称”

[英]MongoDB "Invalid BSON Field Name"

I know that there's probably a better way to do this however I'm completely stumped.我知道可能有更好的方法来做到这一点,但是我完全被难住了。 I'm writing a Discord bot in which a user is able to add points to other users, however I can't figure out how to replace a user's "points".我正在编写一个 Discord 机器人,用户可以在其中向其他用户添加积分,但是我不知道如何替换用户的“积分”。 My code is as follows:我的代码如下:

BasicDBObject cursor = new BasicDBObject();
        cursor.put(user.getAsMember().getId(), getMongoPoints(user.getAsMember()));
        if(cursor.containsKey(user.getAsMember().getId())) {
            Document old = new Document(user.getAsMember().getId(), getMongoPoints(user.getAsMember()));
            Document doc = new Document(user.getAsMember().getId(), getMongoPoints(user.getAsMember()) + Integer.parseInt(amount.getAsString()));
            collection.findOneAndUpdate(old, doc);
        }

My getMongoPoints function:我的 getMongoPoints function:

 public static int getMongoPoints(Member m) {
    ConnectionString connectionString = new ConnectionString("database");
    MongoClientSettings settings = MongoClientSettings.builder()
            .applyConnectionString(connectionString)
            .build();
    MongoClient mongoClient = MongoClients.create(settings);
    MongoDatabase database = mongoClient.getDatabase("SRU");
    MongoCollection<Document> collection = database.getCollection("points");
    DistinctIterable<Integer> docs = collection.distinct(m.getId(), Integer.class);
    MongoCursor<Integer> result = docs.iterator();
    return result.next();
}

I've tried findOneAndReplace, however that simply makes a new entry without deleting the old one.我试过 findOneAndReplace,但是这只是创建一个新条目而不删除旧条目。 The error I receive is: Invalid BSON field name 262014495440896000我收到的错误是: Invalid BSON field name 262014495440896000

Everything else works, include writing to the database itself which is why I'm stumped.其他一切都有效,包括写入数据库本身,这就是我难过的原因。 Any help would be greatly appreciated and I apologize if this is written poorly.任何帮助将不胜感激,如果写得不好,我深表歉意。

BSON field names must be string. BSON 字段名称必须是字符串。 From the spec:从规范:

Zero or more modified UTF-8 encoded characters followed by '\x00'.零个或多个修改后的 UTF-8 编码字符后跟“\x00”。 The (byte*) MUST NOT contain '\x00', hence it is not full UTF-8. (byte*) MUST NOT 包含 '\x00',因此它不是完整的 UTF-8。

To use 262014495440896000 as a field name, convert it to string first.要将262014495440896000用作字段名称,请先将其转换为字符串。

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

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