Java 按_id更新一个文档(无效的BSON字段名_id)

Java 按_id更新一个文档(无效的BSON字段名_id),java,mongodb,Java,Mongodb,我正在尝试使用updateOne方法更新文档: UpdateResult r = coll.updateOne( eq("_id", id), this.getMapper().mapToMongoDocumentEntry(entity) ); 尽管如此,我还是得到了一个例外,告诉我: 无效的BSON字段名\u id mapToMongoDocumentEntity返回一个文档如下: Document{ _id=588b0d7108980f004323ca73, us

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

UpdateResult r = coll.updateOne(
    eq("_id", id),
    this.getMapper().mapToMongoDocumentEntry(entity)
);
尽管如此,我还是得到了一个例外,告诉我:

无效的BSON字段名\u id

mapToMongoDocumentEntity
返回一个
文档
如下:

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

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);
我分享文件的原因是为了引出两条重要条款:

  • updateOne
    readds-要应用的更新必须只包括更新运算符,并且您最好不要更新现有文档的
    \u id
    ,如果您在
    MapTomogoDocumentEntry
    方法中生成该文档,则最好将其替换为一个
  • 由于
    maptomongocumententry
    返回的是整个文档,而不仅仅是属性,因此您实际上需要的是整个文档替换,而不是更新它的字段

  • 另外,请注意,您可以使用上述两种方法,其中重载了一个额外的参数,该参数可以提供对文档
    upsert
    pass
    等的支持。

    您可以发布实体代码吗,请?我还添加了
    MaptomongocumentEntity
    代码。如果您的
    MaptomongocumentEntry
    返回整个
    文档
    ,那么
    replaceOne(Bson筛选器,t文档替换)
    是否对您有帮助?它现在已经工作了。这是
    updateOne
    replaceOne
    之间的区别?非常有用的答案。直到你把它并排展示出来,我才明白更新和替换之间的区别。