作者:世妖娆灬 | 来源:互联网 | 2014-05-28 16:53
假如已经存储了document,现在想往document中添加一个description属性,可以这样更新:Mongomongobh_code_a_Java_keywordsnewMongo(bh_code_a_Java_keywordsthis.host,bh_code_a_Java_keywordsthis.por
假如已经存储了document,现在想往document中添加一个description属性,可以这样更新:
Mongo mongo =
"bh_code_a_Java_keywords">new Mongo(
"bh_code_a_Java_keywords">this.host,
"bh_code_a_Java_keywords">this.port);
GridFS fs =
"bh_code_a_Java_keywords">new GridFS(mongo.getDB("database"));
GridFSDBFile file = fs.find(
"bh_code_a_Java_keywords">new ObjectId(document.getId()));
file.put(
"bh_code_a_Java_string">"description", document.getDescription());
file.save();
也可以使用DBCollection的update方法更新,
DBObject queryCOndition=
"bh_code_a_Java_keywords">new BasicDBObject();
queryCondition.put(
"bh_code_a_Java_string">"_id", document.getId());
Mongo mongo =
"bh_code_a_Java_keywords">new Mongo(
"bh_code_a_Java_keywords">this.host,
"bh_code_a_Java_keywords">this.port);
DB db = mongo.getDB(
"bh_code_a_Java_string">"database")
DBCollection c = db.getCollection(
"bh_code_a_Java_string">"fs.files");
DBObject updatedValue=
"bh_code_a_Java_keywords">new BasicDBObject();
updatedValue.put(
"bh_code_a_Java_string">"description", document.getDescription());
DBObject updateSetValue=
"bh_code_a_Java_keywords">new BasicDBObject(
"bh_code_a_Java_string">"$set",updatedValue);
c.update(updateSetValue, updateSetValue);