MongoDb在其java驱动程序中的$ set等价物 [英] MongoDb's $set equivalent in its java Driver

查看:595
本文介绍了MongoDb在其java驱动程序中的$ set等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以通过Java Driver修改MongoDb中某个键的值。我尝试了以下内容:

Is there a way in which I can modify the value of one of the keys in MongoDb via its Java Driver. I tried out the following:

someCollection.update(DBObject query, DBObject update);
someCollection.findAndModify(DBObject query, DBObject update);

但这两个函数都完全用更新的文档替换查询的文档。在mongo shell中使用$ set的情况下,只更新特定键的一个值的方法是什么。(除了创建一个全新的文档,其中复制了所有字段并更新了其中一个字段)。

But both the functions completely replace the queried document with the updated document. What is the way to update only one of the value of a particular key as in the case of using $set in the mongo shell.(apart from making a completely new Document with all fields copied and one of the fields updated).

推荐答案

我不是java专家,但似乎以下代码符合您的需求:

I am not java expert, but seems following code fit your needs:

BasicDBObject set = new BasicDBObject("$set", new BasicDBObject("age", 10));
set.append("$set", new BasicDBObject("name", "Some Name");
someCollection.update(someSearchQuery, set);

另请参阅这个例子

这篇关于MongoDb在其java驱动程序中的$ set等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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