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

时间:2017-01-27 09:50:13

标签: java mongodb

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

有什么想法吗?

1 个答案:

答案 0 :(得分:7)

/**
 * 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会返回整个文档而不仅仅是属性,因此整个文档替换都是您实际要搜索的,而不是更新它的字段。
  3. 另请注意,您可以使用上述方法重载上额外的参数UpdateOptions,这些参数可以提供对文档upsertbypass等的支持。