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

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

问题描述

参考文献:



mongo db还是很新的,但我试图更新集合中现有文档的一部分......不幸的是,上面的链接没有更新示例。



<基本上,我只希望能够:


  1. 向文档添加新字段

  2. 将文档
    的现有字段更新为新值

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

$ $ p $ code def shape = mongo.shapes.findOne(new BasicDBObject(data,http:// www.foo.com)); //获取文档
mongo.shapes.update(new BasicDBObject(_ id,shape._id),new BasicDBObject(isProcessed,0)); //添加一个新的isProcessed字段设置为0
mongo.shapes.update(新的BasicDBObject(_ id,shape._id),新的BasicDBObject(data,http://www.bar。 COM));

这样几乎可以破坏整个对象......我可能会尝试修改原始形状对象,然后运行更新。但在此之前,没有人有更新单个字段(而不是整个文档)的经验吗?

编辑:



我刚刚尝试过,并且能够通过将新的和/或更新的字段发送到整个对象并成功进行更新。我想知道,这位司机是否足够聪明,只能更新最小的变化部分,或者只是盲目更新整个事情? (在下面的例子中,它只是更新线或整个形状文档中的foo字段?)

代码:

  def shape = mongo.shapes.findOne(); //获得第一个形状作为基础
shape.removeField(_ id); //删除id字段
shape.put(foo,bar); //添加一个新字段foo
mongo.shapes.insert(shape); //插入新形状
def shape2 = mongo.shapes.findOne(new BasicDBObject(foo,bar)); //获取新插入的形状(更重要的是它是id)
shape2.put(foo,bat); //将foo字段更新为新值
mongo.shapes.update(new BasicDBObject(_ id,shape2._id),shape2); //更新现有的mongo文档


解决方案


我想知道,如果驱动程序足够聪明,只能更新最小的一部分更改,或者只是盲目地更新整个内容?



不,如果您使用正常更新方法,整个对象将通过电线发送。
我怀疑数据库服务器本身是否足够聪明,以便只更新必要的索引(而不是那些没有改变的索引),如果可能的话(例如,对象可以更新到位并且不必移动因为它变得太多了)



您可以使用原子更新修饰符功能。 Java文档有点亮,但由于驱动程序只是传输JSON,因此非Java教程中的内容应该可以工作,例如:

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


References:

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.

Essentially, i just want to be able to:

  1. Add new fields to a document
  2. Update existing fields of a document to a new value

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)?

EDIT:

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?)

Code:

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)

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天全站免登陆