也可以使用DBCollection的update方法更新,代码如下:
DBObject queryCondition=new BasicDBObject(); queryCondition.put("_id", document.getId()); Mongo mongo = new Mongo(this.host, this.port); DB db = mongo.getDB("database") DBCollection c = db.getCollection("fs.files"); DBObject updatedValue=new BasicDBObject(); updatedValue.put("description", document.getDescription()); DBObject updateSetValue=new BasicDBObject("$set",updatedValue); /** * update document set description="description" where id='id' * updateCondition:更新条件 * updateSetValue:设置的新值 */ c.update(updateSetValue, updateSetValue);