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

UpdateResult r = coll.updateOne(
    eq("_id", id),
    this.getMapper().mapToMongoDocumentEntry(entity)
);

尽管如此,我还是得到了一个例外,告诉我:

无效的BSON字段名\u 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;
}

有什么 idea 吗?

推荐答案

/**
 * 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-The update to apply must include only update operators,如果你像mapToMongoDocumentEntry方法那样生成一个,那么更新现有文档的_id不是一个好主意,而是用一个替换文档.
  2. 由于mapToMongoDocumentEntry返回的是整个文档,而不仅仅是属性,所以您实际上需要的是整个文档替换,而不是更新它的字段.

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

Mongodb相关问答推荐

使用单个/多个值查询任何/特定,而不使用if块

MongoDB聚合:将文档列表转换为列表

从两个相连的文件中获取电话和邮箱的渠道是什么?

如何将数组$拉到对象数组下

MongoDB获取所有文档谁的S子文档只包含一个特定值?

Kotlin 使用初级构造实例化公开类

如何在查找 foreignField 中使用通配符?

MongoDB 2.1 聚合框架总和匹配名称的数组元素

为什么 PyMongo 会抛出 AutoReconnect?

`fields cannot be identical'和''` mongoimport错误

如何使用原子操作在一个文档中切换布尔字段?

mongo - Ruby连接问题

MongoDB聚合将字符串数组连接到单个字符串

在 mongodb 中插入当前日期时间

Mongo聚合框架,排序然后分组不起作用

有人在 Google App Engine 上try 过 MongoDB 吗?

获取 mongodb 中所有唯一标签的列表

RoboMongo:不显示所有文档

如何在 $match 内的 mongodb 聚合查询中使用 $regex

将新值推送到 mongodb 内部数组 - mongodb/php