2
我試圖用updateOne
方法來更新文件:更新一個文檔的_id(無效BSON字段名_id)
UpdateResult r = coll.updateOne(
eq("_id", id),
this.getMapper().mapToMongoDocumentEntry(entity)
);
不過,我得到一個異常告訴我:
無效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
}
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;
}
任何想法?
你可以發佈實體代碼,請? –
我也添加了'mapToMongoDocumentEntity'代碼。 – Jordi
如果你的'mapToMongoDocumentEntry'返回整個'Document','replaceOne(Bson過濾器,TDocument替換)'幫助你嗎? – nullpointer