数据库

 首页 > 数据库 > MongoDB > MongoDB数据库的update api接口使用说明

MongoDB数据库的update api接口使用说明

分享到:
【字体:
导读:
         摘要:从两方面来说明。shell命令行说明,java客户端api说明;java客户端例子:setconn.prepareStatement(querysql+fromsql).executeQuery();while(set.next()){DBObjectqnewBasicDBObject().append(T,a).ap...

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( criteriaobjNewupsertmulti )

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接口使用说明
分享到:
MongoDB数据库的基本语句使用方法
MongoDB数据库的基本语句使用方法 查询: MySQL: SELECT * FROM user Mongo: db.user.find() 带条件的查询: MySQL: SELECT * FROM user WHERE name = ’starlee’ Mongo: db.user.find({‘name’ : ’starlee’}) 插入: MySQL: INSERT INOT user (`name`, `age`) v...
MongoDB与GridFS文件系统的使用方法
MongoDB与GridFS文件系统的使用方法     GridFS 是一种将大型文件存储在 MongoDB 数据库中的文件规范。所有官方支持的驱动均实现了 GridFS 规范。 1 为什么要用 GridFS     由于 MongoDB 中 BSON 对象大小是有限制的,所以 GridFS 规范提供了一种透明的机制,可 以将一个大文件分割成为多个较小的文档, 这样的机制...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……