如何使用java驱动程序更新mongo db中的文档字段?

lon*_*gda 18 java grails groovy mongodb mongo-java

参考文献:

仍然是mongo db的新手,但我正在尝试更新集合中现有文档的一部分......遗憾的是,上面的链接没有更新示例.

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

  1. 向文档添加新字段
  2. 将文档的现有字段更新为新值

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

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"));
Run Code Online (Sandbox Code Playgroud)

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

编辑:

我只是尝试了它,并且能够通过发送整个对象以及新的和/或更新的字段来成功更新.我想知道驱动程序是否足够聪明,只更新最小的更改子集,或者只是盲目更新整个事物?(在下面的例子中,它只是更新电线或整个形状文档的foo字段?)

码:

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
Run Code Online (Sandbox Code Playgroud)

Thi*_*ilo 12

我想知道驱动程序是否足够聪明,只更新最小的更改子集,或者只是盲目更新整个事物?

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

你可以做的是使用"原子更新修饰符"功能.Java文档是对他们有点轻,但由于驾驶者只要发送JSON,从非Java教程的东西应该工作,例如:

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


tim*_*tes 11

在这里找到一个示例,它似乎显示了更新调用的用法.所以对于你的例子,我相信这样的事情应该有用吗?

// Find an object
def shape2 = mongo.shapes.findOne( new BasicDBObject( 'foo', 'bar' ) )
// And update the foo field from 'bar' to 'bat'
mongo.shapes.update( shape2, new BasicDBObject( '$set', new BasicDBObject( 'foo', 'bat' ) ) )
Run Code Online (Sandbox Code Playgroud)

编辑

您可以使用类别以更加时髦的方式构建BasicDBObjects ...

这样的事情可能会这样:

class BasicDBObjectMapBuilder {
  static String toDbObj( String s ) { s }
  static BasicDBObject toDbObj( Map m ) {
    m.inject( null ) { r, it -> new BasicDBObject( it.key, it.value.toDbObj() ) }
  }
}

use( BasicDBObjectMapBuilder ) {
  def shape2 = mongo.shapes.findOne( new BasicDBObject( 'foo', 'bar' ) )
  // And update the foo field from 'bar' to 'bat'
  mongo.shapes.update( shape2, [ '$set':[ 'foo', 'bat' ] ].toDbObj() )
}
Run Code Online (Sandbox Code Playgroud)

我没有测试过这个......

编辑2

实际上,BasicDBObject是一个Map,所以你应该能够做到:

  mongo.shapes.update( shape2, [ '$set':[ 'foo', 'bat' ] ] as BasicDBObject )
Run Code Online (Sandbox Code Playgroud)

不需要建设者


rob*_*ins 5

这篇文章的很多答案都是使用旧版本的Mongo Java Driver.如果您使用的是较新版本的Java驱动程序(v3.0 +),则首选方法似乎是使用Document对象而不是DBObject接口.

这是一个例子:

MongoClient client = new MongoClient();
MongoCollection<Document> fooCollection = client.getDatabase("test").getCollection("foo");

Bson filter = Filters.eq("_id", "123d45678c467bb433c99f99");
Bson updates = Updates.set("isFoo", true);
fooCollection.findOneAndUpdate(filter, updates);
Run Code Online (Sandbox Code Playgroud)