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



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

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;
}

有什么想法吗?

/**
 * 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重新添加 - 要应用的更新必须仅包含更新运算符,如果您像在mapToMongoDocumentEntry方法中那样生成文档,则更新现有文档的_id而不是用文档替换文档不是一个好主意。
  2. 由于mapToMongoDocumentEntry返回整个文档,而不仅仅是属性,因此整个文档替换是您实际要寻找的,而不是更新其字段。

另外,请注意,您可以使用上述两种方法重载一个额外的参数UpdateOptions该参数可以为文档upsertbypass等提供支持。

最新更新