在mongo 3.3中等效的DBCollection save() [英] DBCollection save() equivalent in mongo 3.3

查看:336
本文介绍了在mongo 3.3中等效的DBCollection save()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前使用mongo-java-driver:3.0.4进行文档更新的实现如下-

Our current implementation with mongo-java-driver:3.0.4 for a document update is as follows -

private void updateParam(String param1, String param2) {
    Param param = new Param(param1, param2);
    DBCollection collection = mongoClient.getDB("databaseName").getCollection("collectionName");
    collection.save(new BasicDBObject(param.toMap()));
}

其中param.toMap()被实现为

public Map<String, Object> toMap() {
    return JSONObjectMapper.getObjectMapper().convertValue(this, Map.class);
}


mongo-java-driver:3.4.0-rc1中,我尝试的实现方式是在insertOne中使用


With mongo-java-driver:3.4.0-rc1 the implementation I tried was using insertOne as

private void updateParam(String param1, String param2) {
    Param param = new Param(param1, param2);
    MongoCollection<Param> mongoCollection = mongoClient.getDatabase("databaseName").getCollection("collectionName", Param.class);
    mongoCollection.insertOne(param);
}

考虑来自源 DBCollection的保存

  • 如果不存在具有指定'_id'值的文档,则 方法使用文档中的指定字段执行插入.
  • 如果存在具有指定'_id'值的文档,则该方法 执行更新,将现有记录中的所有字段替换为 文档中的字段.
  • If a document does not exist with the specified '_id' value, the method performs an insert with the specified fields in the document.
  • If a document exists with the specified '_id' value, the method performs an update, replacing all field in the existing record with the fields from the document.

但是我怀疑 insertOne 实现,现在我将其用于与以前save()类似的效果.

But I doubt the insertOne implementation now that I am using it for the similar effect as to the save() previously.

插入提供的文档.如果文档丢失 标识符,驱动程序应生成一个

Inserts the provided document. If the document is missing an identifier, the driver should generate one

问题 -使用当前的MongoCollection方法是否与save()类似?有没有办法使用Param中的_id或不使用它而做类似的事情?

Question - Is there any similar way as of save() with current MongoCollection approach? Is there a way to use _id of Param or do something similar without using it as well?

参数定义为-

Edit : Param is defined as -

public class Param {

  private String param2;
  private String param2;

  public Param() {
  }

  public Param(String param1, String param2) {
    this.param1 = param1;
    this.param2 = param2;
  }

  public Map<String, Object> toMap() {
    return JSONObjectMapper.getObjectMapper().convertValue(this, Map.class);
  }

}

并具有ParamCodec implements Codec<Param>,该ParamCodec implements Codec<Param>使用CodecRegistry如下注册到客户端:

and has a ParamCodec implements Codec<Param> which is registered using CodecRegistry as follows to the client :

CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(),
            CodecRegistries.fromCodecs(new ParamCodec()));

MongoClientOptions clientOptions = new MongoClientOptions.Builder()
    ....
    .codecRegistry(codecRegistry)
    .build();

MongoClient(addresses, clientOptions);

推荐答案

您可能想使用

findOneAndUpdate(Bson filter, Bson update, FindOneAndUpdateOptions options)

带有upsert选项设置为true的FindOneAndUpdateOptions.

with FindOneAndUpdateOptions with upsert option set to true.

您可以传递_id作为查询过滤器,并传递param作为更新,当查询与_id匹配时,它将向上插入该值,如果找不到匹配项,则将插入新行.

You can pass the _id as the query filter and param as your update and when the query matches the _id it will upsert the value and if no match is found it will insert a new row.

这篇关于在mongo 3.3中等效的DBCollection save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆