2017-01-27 116 views
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; 
} 

任何想法?

+0

你可以發佈實體代碼,請? –

+0

我也添加了'mapToMongoDocumentEntity'代碼。 – Jordi

+2

如果你的'mapToMongoDocumentEntry'返回整個'Document','replaceOne(Bson過濾器,TDocument替換)'幫助你嗎? – nullpointer

回答

4
/** 
* 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返回整個文檔,而不僅僅是屬性的整個文檔替換是你實際上正在尋找,而不是更新它的領域。

另外請注意,您可以同時使用一個額外PARAM UpdateOptions可用於文檔upsert,提供支持重載上述方法bypass