MongoDB数据库的update api接口使用说明
从两方面来说明。 shell命令行说明,java 客户端api说明;
java客户端例子:
set = conn.prepareStatement(querysql+fromsql).executeQuery() ;
while(set.next()){
DBObject q = new BasicDBObject().append("T", "a")
.append("CI", String.valueOf(set.getInt(1)))
.append("AI", String.valueOf(set.getInt(3)));
DBObject o = new BasicDBObject().append("$set",
new BasicDBObject().append("CN", set.getString(2))
.append("AN", set.getString(4)).append("S", set.getString(5)));
Mongodb.getDB("yqflog").getCollection("info").update(q, o , true , true );
}
mongodb的java客户端的api:
/**
* calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean, com.mongodb.WriteConcern)} with default WriteConcern.
* @param q search query for old object to update
* @param o object with which to update q
* @param upsert if the database should create the element if it does not exist
* @param multi if the update should be applied to all objects matching (db version 1.1.3 and above)
* See http://www.mongodb.org/display/DOCS/Atomic+Operations
* @return
* @throws MongoException
* @dochub update
*/
public WriteResult update( DBObject q , DBObject o , boolean upsert , boolean multi )
throws MongoException {
return update( q , o , upsert , multi , getWriteConcern() );
}
shell命令行:
db.collection.update( criteria, objNew, upsert, multi )
Arguments:
criteria - query which selects the record to update;
objNew - updated object or $ operators (e.g., $inc) which manipulate the object
upsert - if this should be an "upsert" operation; that is, if the record(s) do not exist, insert one. Upsert only inserts a single document.
multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.
$options 操作说明:
实例中使用了$set,否则就会丢失原有值;
$inc{ $inc : { field : value } }
increments field by the number value if field is present in the object, otherwise sets field to the number value. This can also be used to decrement by using a negative value.
$set{ $set : { field : value } }
sets field to value. All datatypes are supported with $set.
$unset{ $unset : { field : 1} }
Deletes a given field. v1.3+
MongoDB数据库的update api接口使用说明