【发布时间】:2014-02-19 20:04:15
【问题描述】:
我看到了其他几个问题,但其中任何一个都解决了我的问题。
我该怎么做:
UPDATE table SET fiel1 = 'a', field2 = 'b', field3 = 'c' WHERE id='111'
在MongoDB中使用java驱动?
【问题讨论】:
标签: mongodb mongodb-java
我看到了其他几个问题,但其中任何一个都解决了我的问题。
我该怎么做:
UPDATE table SET fiel1 = 'a', field2 = 'b', field3 = 'c' WHERE id='111'
在MongoDB中使用java驱动?
【问题讨论】:
标签: mongodb mongodb-java
首先,我认为您需要了解 Mongo shell 脚本的外观。 您的类似 SQL 的查询将转换为以下内容:
db.table.update({id : '111'},{$set : {fiel1 : 'a', field2 : 'b', field3 : 'c'}})
使用 Java 驱动程序,您将需要以下内容:
//obtain the collection object:
DBCollection coll = db.getCollection("table"); //I assume you create your DB-typed db object before
//query DB Object
DBObject query = new BasicDBObject("id", "111");
//nested DB Object of update object
DBObject setObj = new BasicDBObject();
setObj.put("fiel1", "a");
setObj.put("field2", "b");
setObj.put("field3", "c");
//update DB Object
DBObject update = new BasicDBObject("$set", setObj);
coll.update(query, update);
【讨论】:
coll.update(query, update);