如何使用 java 驱动程序更新 mongo db 中的文档字段? [英] How do I update fields of documents in mongo db using the java driver?

查看:21
本文介绍了如何使用 java 驱动程序更新 mongo db 中的文档字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考文献:

对 mongo db 来说仍然很新,但我正在尝试更新集合中现有文档的一部分……不幸的是,上面的链接没有更新示例.

Still pretty new to mongo db but I'm trying to update part of an existing document inside a collection... unfortunately, the link above doesn't have an update example.

本质上,我只想能够:

  1. 向文档添加新字段
  2. 更新文档的现有字段到一个新的值

这是我的代码(Grails + Groovy + Java + MongoDB + java 驱动程序):

Here's my code (Grails + Groovy + Java + MongoDB + the java driver):

def shape = mongo.shapes.findOne(new BasicDBObject("data", "http://www.foo.com")); // get the document
mongo.shapes.update(new BasicDBObject("_id", shape._id), new BasicDBObject("isProcessed", 0));  // add a new "isProcessed" field set to 0
mongo.shapes.update(new BasicDBObject("_id", shape._id), new BasicDBObject("data", "http://www.bar.com"));

这几乎破坏了整个对象...我可能会尝试只修改原始形状对象,然后在其上运行更新.但在那之前,有没有人有过只更新个别字段(而不是整个文档)的经验?

This pretty much clobbers the entire object... I might try just modifying the original shape object and then running the update on that. But until then, does anyone have experience updating just individual fields (rather than the entire document)?

我刚刚尝试过,并且能够通过将整个对象与新的和/或更新的字段一起发送来成功更新,并且有效.我想知道驱动程序是否足够聪明,只能更新最小的更改子集,还是只是盲目地更新整个事情?(在下面的例子中,它只是更新了线路上的 foo 字段还是整个形状文档?)

I just tried it and was able to successfully update by sending the entire object across with new and/or updated fields and that works. I wonder if the driver is smart enough to only update the smallest subset of changes or if it's just blindly updating the entire thing? (In the case below, is it just updating the foo field across the wire or the entire shape document?)

代码:

def shape = mongo.shapes.findOne(); // get the first shape to use as a base
shape.removeField("_id");  // remove the id field
shape.put("foo","bar");  // add a new field "foo"
mongo.shapes.insert(shape);  // insert the new shape
def shape2 = mongo.shapes.findOne(new BasicDBObject("foo", "bar"));  // get the newly inserted shape (and more importantly, it's id)
shape2.put("foo", "bat");  // update the "foo" field to a new value
mongo.shapes.update(new BasicDBObject("_id", shape2._id), shape2);  // update the existing document in mongo

推荐答案

我想知道驱动程序是否足够聪明,可以只更新最小的更改子集,还是只是盲目地更新整个内容?

I wonder if the driver is smart enough to only update the smallest subset of changes or if it's just blindly updating the entire thing?

不,如果您使用正常"更新方法,整个对象将通过网络发送.我怀疑如果可能的话,数据库服务器本身会足够聪明,只更新必要的索引(而不是那些没有改变的索引)(即对象可以就地更新并且不必因为它增长太多而移动)很多)

No, if you use the "normal" update method, the whole object will be sent over the wire. I suspect that the database server itself will be clever enough to only update the necessary indexes (and not the ones that did not change), if possible (i.e. the object could be updated in place and did not have to be moved because it grew too much)

您可以做的是使用原子更新修饰符"功能.Java 文档对它们有些了解,但由于驱动程序只是传输 JSON,因此非 Java 教程中的内容应该可以工作,例如:

What you can do is use the "atomic update modifier" functions. The Java documentation is a bit light on them, but since the driver just transmits the JSON, the stuff from the non-Java tutorials should work, for example:

shapes.update((DBObject)JSON.parse(    "{ 'foo' : 'bar'}"),  
    (DBObject) JSON.parse(          "{ '$set' : { 'foo': 'bat'}}")   );

这篇关于如何使用 java 驱动程序更新 mongo db 中的文档字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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