我正在尝试使用updateOne方法更新文档:
UpdateResult r = coll.updateOne(
eq("_id", id),
this.getMapper().mapToMongoDocumentEntry(entity)
);
Run Code Online (Sandbox Code Playgroud)
不过,我得到一个例外告诉我:
无效的BSON字段名称_id
mapToMongoDocumentEntity返回一个Document像:
Document{
_id=588b0d7108980f004323ca73,
username=user,
password=.---,
cname=----,
sname=,
mail=mail,
creation=Fri Jan 27 09:05:52 UTC 2017,
validation=null
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
/**
* 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);
Run Code Online (Sandbox Code Playgroud)
应该比你好
/**
* 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);
Run Code Online (Sandbox Code Playgroud)
我分享文件的原因是提出两个重要条款 -
updateOnereadds - 要应用的更新必须仅包括更新运算符,_id如果您在mapToMongoDocumentEntry方法中生成文档,则更新现有文档并不更换文档,这不是一个好主意.mapToMongoDocumentEntry返回整个文档而不仅仅是属性,因此整个文档替换是您实际寻求的而不是更新它的字段.另外请注意,您可以同时使用一个额外PARAM重载上面的方法UpdateOptions,可以为文件提供了支持upsert,bypass等等.