- /**
- * MongoDBTest
- * MongoDB更新数据使用示例
- */
- package com.labci.mongodb.test;
- import java.net.UnknownHostException;
- import java.util.Iterator;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.Mongo;
- import com.mongodb.MongoException;
- /**
- *
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 22, 2011[3:10:56 PM]
- *
- */
- public class MongoDBUpdateDataTest {
- private static final String HOST = "192.168.1.86";
- private static final int PORT = 27017;
- private static final String USER = "iwtxokhtd";
- private static final String PASSWORD = "123456";
- private static final String DB_NAME = "test";
- private static final String COLLECTION = "insert_test";
- private static Mongo conn=null;
- private static DB myDB=null;
- private static DBCollection myCollection=null;
- static{
- try {
- conn=new Mongo(HOST,PORT);//建立数据库连接
- myDB=conn.getDB(DB_NAME);//使用test数据库
- boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证
- if(loginSuccess){
- myCollection=myDB.getCollection(COLLECTION);
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (MongoException e) {
- e.printStackTrace();
- }
- }
- /**
- * 更新“表”数据
- * @param collection
- */
- private static void updateData(DBCollection collection){
- DBObject updateCondition=new BasicDBObject();
- //where name='fox'
- updateCondition.put("name", "fox");
- DBObject updatedValue=new BasicDBObject();
- updatedValue.put("headers", 3);
- updatedValue.put("legs", 4);
- DBObject updateSetValue=new BasicDBObject("$set",updatedValue);
- /**
- * update insert_test set headers=3 and legs=4 where name='fox'
- * updateCondition:更新条件
- * updateSetValue:设置的新值
- */
- collection.update(updateCondition, updateSetValue);
- DBObject queryCondition=new BasicDBObject();
- //where name='sam',此条件在更新前不是成立的
- queryCondition.put("name", "sam");
- DBObject setValue=new BasicDBObject();
- setValue.put("headers", 1);
- setValue.put("legs", 1);
- DBObject upsertValue=new BasicDBObject("$set",setValue);
- /**
- * 后面两个参数含义分别是:
- * 若所更新的数据没有,则插入
- * ,同时更新多个符合条件的文档(collection)
- */
- collection.update(queryCondition, upsertValue, true, true);
- //set headers=headers+2
- DBObject incValue=new BasicDBObject("headers",2);
- //set legs=4
- DBObject legsValue=new BasicDBObject("legs",4);
- DBObject allCondition=new BasicDBObject();
- allCondition.put("$inc", incValue);
- allCondition.put("$set", legsValue);
- collection.update(queryCondition, allCondition);
- }
- /**
- * 返回查询结果集
- * @param collection
- * @return
- */
- private static DBCursor queryData(DBCollection collection){
- DBCursor queriedData=collection.find();
- return queriedData;
- }
- /**
- * 打印结果数据
- * @param description 结果数据相关描述
- * @param recordResult 结果集
- */
- private static void printData(String description,DBCursor recordResult){
- System.out.println(description);
- for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){
- System.out.println(iter.next());
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- printData("查看一下更新前的数据:",queryData(myCollection));
- //更新数据
- updateData(myCollection);
- printData("查看一下更新后的数据:",queryData(myCollection));
- }
- }
运行结果:
- 查看一下更新前的数据:
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}
- { "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 1 , "legs" : 1 , "name" : "sam"}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height" : 16.3 , "weight" : 22} , "legs" : 4 , "userName" : "iwtxokhtd"}
- 查看一下更新后的数据:
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}
- { "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 3 , "legs" : 4 , "name" : "sam"}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height" : 16.3 , "weight" : 22} , "legs" : 4 , "userName" : "iwtxokhtd"}