【问题标题】:Update one document by _id (Invalid BSON field name _id)通过 _id 更新一个文档(无效的 BSON 字段名称 _id)
【发布时间】:2017-01-27 19:20:12
【问题描述】:

我正在尝试使用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;
}

有什么想法吗?

【问题讨论】:

  • 能否贴出实体代码?
  • 我还添加了mapToMongoDocumentEntity 代码。
  • 如果您的mapToMongoDocumentEntry 返回整个DocumentreplaceOne(Bson filter, TDocument replacement) 对您有帮助吗?
  • 它现在已经工作了。 updateOnereplaceOne 有什么区别?

标签: java mongodb


【解决方案1】:
/**
 * 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 返回整个文档而不仅仅是属性,因此您实际需要的是替换整个文档而不是更新它的字段。

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

【讨论】:

  • 非常有用的答案。在您并排显示之前,我没有理解更新和替换之间的区别。
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 2011-04-30
  • 2020-11-27
  • 1970-01-01
  • 2014-06-01
相关资源
最近更新 更多