[英]Update one document by _id (Invalid BSON field name _id)
I'm trying to update an document using updateOne
method: 我正在尝试使用
updateOne
方法更新文档:
UpdateResult r = coll.updateOne(
eq("_id", id),
this.getMapper().mapToMongoDocumentEntry(entity)
);
Nevertheless, I'm getting an exception telling me: 不过,我得到一个例外告诉我:
Invalid BSON field name _id
无效的BSON字段名称_id
mapToMongoDocumentEntity
returns a Document
like: mapToMongoDocumentEntity
返回一个Document
如:
Document{
_id=588b0d7108980f004323ca73,
username=user,
password=.---,
cname=----,
sname=,
mail=mail,
creation=Fri Jan 27 09:05:52 UTC 2017,
validation=null
}
mapToMongoDocumentEntry
code: 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;
}
Any ideas? 有任何想法吗?
/**
* 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);
should work well for you than the 应该比你好
/**
* 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);
The reasons I shared the documentation is to bring out two important clauses - 我分享文件的原因是提出两个重要条款 -
updateOne
readds - The update to apply must include only update operators and you its not a good idea to update the _id
of an existing document rather replace the document with one if you are generating that as in your mapToMongoDocumentEntry
method. updateOne
readds - 要应用的更新必须仅包含更新运算符 ,如果您在mapToMongoDocumentEntry
方法中生成文档,则更新现有文档的_id
而不是将文档替换为1。 mapToMongoDocumentEntry
returns the entire document and not just attributes the entire Document replacement is what you are actually seeking rather thatn updating fields of it. mapToMongoDocumentEntry
返回整个文档而不仅仅是属性,因此整个Document替换是您实际要搜索的内容,而不是更新它的字段。 Also, note that you can use both the above methods overloaded with an additional param UpdateOptions
which can provide the support for document upsert
, bypass
etc. 另请注意,您可以使用上述方法重载其他参数
UpdateOptions
,这些参数可以提供对文档upsert
, bypass
等的支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.