MongoDB JAVA API更新数据示例

来源:互联网 发布:大数据系统 编辑:程序博客网 时间:2024/05/29 07:36
[java] view plaincopyprint?
  1. /** 
  2.  * MongoDBTest 
  3.  * MongoDB更新数据使用示例 
  4.  */  
  5. package com.labci.mongodb.test;  
  6. import java.net.UnknownHostException;  
  7. import java.util.Iterator;  
  8. import com.mongodb.BasicDBObject;  
  9. import com.mongodb.DB;  
  10. import com.mongodb.DBCollection;  
  11. import com.mongodb.DBCursor;  
  12. import com.mongodb.DBObject;  
  13. import com.mongodb.Mongo;  
  14. import com.mongodb.MongoException;  
  15. /** 
  16.  *  
  17.  * @author Bill Tu(tujiyue/iwtxokhtd) 
  18.  * May 22, 2011[3:10:56 PM] 
  19.  * 
  20.  */  
  21. public class MongoDBUpdateDataTest {  
  22.     private static final String HOST = "192.168.1.86";  
  23.     private static final int PORT = 27017;  
  24.     private static final String USER = "iwtxokhtd";  
  25.     private static final String PASSWORD = "123456";  
  26.     private static final String DB_NAME = "test";  
  27.     private static final String COLLECTION = "insert_test";  
  28.     private static Mongo conn=null;  
  29.     private static DB myDB=null;  
  30.     private static DBCollection myCollection=null;  
  31.       
  32.     static{  
  33.         try {  
  34.             conn=new Mongo(HOST,PORT);//建立数据库连接  
  35.             myDB=conn.getDB(DB_NAME);//使用test数据库  
  36.             boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证  
  37.             if(loginSuccess){  
  38.                 myCollection=myDB.getCollection(COLLECTION);  
  39.             }  
  40.         } catch (UnknownHostException e) {  
  41.             e.printStackTrace();  
  42.         } catch (MongoException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46.       
  47.     /** 
  48.      * 更新“表”数据 
  49.      * @param collection 
  50.      */  
  51.     private static void updateData(DBCollection collection){  
  52.         DBObject updateCondition=new BasicDBObject();  
  53.           
  54.         //where name='fox'  
  55.         updateCondition.put("name""fox");  
  56.           
  57.         DBObject updatedValue=new BasicDBObject();  
  58.         updatedValue.put("headers"3);  
  59.         updatedValue.put("legs"4);  
  60.           
  61.         DBObject updateSetValue=new BasicDBObject("$set",updatedValue);  
  62.         /** 
  63.          * update insert_test set headers=3 and legs=4 where name='fox' 
  64.          * updateCondition:更新条件 
  65.          * updateSetValue:设置的新值 
  66.          */  
  67.         collection.update(updateCondition, updateSetValue);  
  68.           
  69.         DBObject queryCondition=new BasicDBObject();  
  70.           
  71.         //where name='sam',此条件在更新前不是成立的  
  72.         queryCondition.put("name""sam");  
  73.           
  74.         DBObject setValue=new BasicDBObject();  
  75.         setValue.put("headers"1);  
  76.         setValue.put("legs"1);  
  77.           
  78.         DBObject upsertValue=new BasicDBObject("$set",setValue);  
  79.         /** 
  80.          * 后面两个参数含义分别是: 
  81.          * 若所更新的数据没有,则插入 
  82.          * ,同时更新多个符合条件的文档(collection) 
  83.          */  
  84.         collection.update(queryCondition, upsertValue, truetrue);  
  85.         //set headers=headers+2  
  86.         DBObject incValue=new BasicDBObject("headers",2);  
  87.         //set legs=4  
  88.         DBObject legsValue=new BasicDBObject("legs",4);  
  89.           
  90.         DBObject allCondition=new BasicDBObject();  
  91.         allCondition.put("$inc", incValue);  
  92.         allCondition.put("$set", legsValue);  
  93.           
  94.         collection.update(queryCondition, allCondition);  
  95.     }  
  96.       
  97.     /** 
  98.      * 返回查询结果集 
  99.      * @param collection 
  100.      * @return 
  101.      */  
  102.     private static DBCursor queryData(DBCollection collection){  
  103.         DBCursor queriedData=collection.find();  
  104.         return queriedData;  
  105.           
  106.     }  
  107.       
  108.     /** 
  109.      * 打印结果数据 
  110.      * @param description 结果数据相关描述 
  111.      * @param recordResult 结果集 
  112.      */  
  113.     private static void printData(String description,DBCursor recordResult){  
  114.         System.out.println(description);  
  115.         for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){  
  116.             System.out.println(iter.next());  
  117.         }  
  118.     }  
  119.       
  120.       
  121.     /** 
  122.      * @param args 
  123.      */  
  124.     public static void main(String[] args) {  
  125.       
  126.         printData("查看一下更新前的数据:",queryData(myCollection));  
  127.         //更新数据  
  128.         updateData(myCollection);  
  129.         printData("查看一下更新后的数据:",queryData(myCollection));  
  130.           
  131.     }  
  132. }  

运行结果:

[java] view plaincopyprint?
  1. 查看一下更新前的数据:  
  2. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}  
  3. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}  
  4. "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 1 , "legs" : 1 , "name" : "sam"}  
  5. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height" : 16.3 , "weight" : 22} , "legs" : 4 , "userName" : "iwtxokhtd"}  
  6. 查看一下更新后的数据:  
  7. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}  
  8. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}  
  9. "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 3 , "legs" : 4 , "name" : "sam"}  
  10. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height" : 16.3 , "weight" : 22} , "legs" : 4 , "userName" : "iwtxokhtd"}