gpt4 book ai didi

java - 通过 _id 更新一个文档(无效的 BSON 字段名称 _id)

转载 作者:IT老高 更新时间:2023-10-28 13:21:09 24 4
gpt4 key购买 nike

我正在尝试使用 updateOne 方法更新文档:

UpdateResult r = coll.updateOne(
eq("_id", id),
this.getMapper().mapToMongoDocumentEntry(entity)
);

尽管如此,我收到一个异常告诉我:

Invalid BSON field name _id

mapToMongoDocumentEntity 返回一个 Document 像:

Document{
_id=588b0d7108980f004323ca73,
username=user,
password=.---,
cname=----,
sname=,
mail=mail,
creation=Fri Jan 27 09:05:52 UTC 2017,
validation=null
}

mapToMongoDocumentEntry 代码:

public Document mapToMongoDocumentEntry(User entity) {
Document result = new Document();

if (entity.getId() != null)
result.put(UserEntityMongoDocumentMapper.FIELD_ID, new ObjectId(entity.getId()));

result.put(UserEntityMongoDocumentMapper.FIELD_USER, entity.getUser());
result.put(UserEntityMongoDocumentMapper.FIELD_PASSWORD, entity.getPasswd());
result.put(UserEntityMongoDocumentMapper.FIELD_COMMONNAME, entity.getCname());
result.put(UserEntityMongoDocumentMapper.FIELD_SURNAME, entity.getSname());
result.put(UserEntityMongoDocumentMapper.FIELD_MAIL, entity.getMail());
result.put(UserEntityMongoDocumentMapper.FIELD_CREATION, entity.getCreation());
result.put(UserEntityMongoDocumentMapper.FIELD_VALIDATION, entity.getValidation());

return result;
}

有什么想法吗?

最佳答案

/**
* Replace a document in the collection according to the specified arguments.
*
* @param filter the query filter to apply the the replace operation
* @param replacement the replacement document
* @return the result of the replace one operation
* @throws com.mongodb.MongoWriteException if the write failed due some other failure specific to the replace command
* @throws com.mongodb.MongoWriteConcernException if the write failed due being unable to fulfil the write concern
* @throws com.mongodb.MongoException if the write failed due some other failure
* @mongodb.driver.manual tutorial/modify-documents/#replace-the-document Replace
*/
UpdateResult replaceOne(Bson filter, TDocument replacement);

应该比你更适合

/**
* Update a single document in the collection according to the specified arguments.
*
* @param filter a document describing the query filter, which may not be null.
* @param update a document describing the update, which may not be null. The update to apply must include only update operators.
* @return the result of the update one operation
* @throws com.mongodb.MongoWriteException if the write failed due some other failure specific to the update command
* @throws com.mongodb.MongoWriteConcernException if the write failed due being unable to fulfil the write concern
* @throws com.mongodb.MongoException if the write failed due some other failure
* @mongodb.driver.manual tutorial/modify-documents/ Updates
* @mongodb.driver.manual reference/operator/update/ Update Operators
*/
UpdateResult updateOne(Bson filter, Bson update);

我分享文档的原因是为了引出两个重要的条款 -

  1. updateOne readds - 要应用的更新必须只包含更新操作符,而且更新现有的 _id 不是一个好主意如果您像在 mapToMongoDocumentEntry 方法中那样生成文档,请使用文档替换文档。
  2. 由于 mapToMongoDocumentEntry 返回整个文档,而不仅仅是属性,整个文档替换是您实际寻求的,而不是更新它的字段。

另外,请注意,您可以使用上述两种方法重载一个额外的参数 UpdateOptions可以提供对文档upsertbypass等的支持

关于java - 通过 _id 更新一个文档(无效的 BSON 字段名称 _id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41891057/

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