1024programmer Mongodb Instructions for using updateapi interface of MongoDB database

Instructions for using updateapi interface of MongoDB database

From two aspects to explain. Shell command line description, java client API description;


java client example:

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 client api:

/**

* calls {@link
DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject,
boolean, boolean, com.mongodb.WriteConcern)} with default
Write Concern.

* @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 command line:

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 operation instructions:

$set is used in the example, otherwise the original value will be lost;

$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+

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/instructions-for-using-updateapi-interface-of-mongodb-database/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索