1. 程式人生 > >java操作mongoDB之增刪改查

java操作mongoDB之增刪改查

需要mongodb的驅動jar(mongo-java-driver-3.2.0.jar) 
package mongodb; 

import java.net.UnknownHostException;  
import java.util.Date;  
import com.mongodb.BasicDBObject;  
import com.mongodb.DB;  
import com.mongodb.DBCollection;  
import com.mongodb.DBCursor;  
import com.mongodb.Mongo;  
import com.mongodb.MongoException; 
import com.mongodb.util.JSON; 

public class mongoDB_zsgc { 
@SuppressWarnings("deprecation") 
public static void main(String[] args) throws UnknownHostException {  
  
        try {  
  
            /**** 連線資料庫 ****/  
            Mongo mongo = new Mongo("127.0.0.1",27017);  
              
            
            /****獲取資料庫 ****/  
            // if database doesn't exists, MongoDB will create it for you  
            DB db = mongo.getDB("userMessageDB");  

            /****獲取資料集合 ****/  
            // if collection doesn't exists, MongoDB will create it for you  
            DBCollection table = db.getCollection("userList");  
  
            /**** 新增資料(10條) ****/   
//            BasicDBObject document =null; 
//            for(int i =1;i<=10;i++){ 
//              document = new BasicDBObject(); 
//              document.put("name", "student"+i);  
//                  document.put("age", 18);  
//                  document.put("createdDate", new Date()); 
//                  table.insert(document); 
//            } 
             
  
            /****單條查詢資料1****/  
//            BasicDBObject searchQuery = new BasicDBObject();  
//            searchQuery.put("name", "student1");  
//            DBCursor cursor = table.find(searchQuery);  
//            while (cursor.hasNext()) {  
//                System.out.println(cursor.next());  
//            } 
            /****單條查詢資料2 ****/  
//            BasicDBObject searchQuery3 = new BasicDBObject().append("name", "student1");  
//            DBCursor cursor3 = table.find(searchQuery3);  
//            while (cursor3.hasNext()) {  
//                System.out.println(cursor3.next());  
//            }  
       
            //查詢所有的資料庫 
//            for (String db1 : mongo.getDatabaseNames()) {  
//                System.out.println(db1);  
//            }  
//            //查詢所有的聚集集合  
//            for (String name : db.getCollectionNames()) {  
//                System.out.println(name);  
//            }  
            
            
            
          
  
            /****修改資料****/  
//            BasicDBObject query4 = new BasicDBObject();  
//            query4.put("name", "student10");  
//  
//            BasicDBObject newDocument = new BasicDBObject();  
//            newDocument.put("name", "kaige");  
//  
//            BasicDBObject updateObj = new BasicDBObject();  
//            updateObj.put("$set", newDocument);  
//            table.update(query4, updateObj);  
//            
            //刪除資料 
            deleteColData(table, "student1"); 
            //查詢所有資料  
            DBCursor cursor = table.find();  
            while (cursor.hasNext()) {  
                System.out.println(cursor.next());  
            }  
            System.out.println(cursor.count());  
            System.out.println(cursor.getCursorId());  
            System.out.println(JSON.serialize(cursor)); 
            
            
           

  
        } catch (MongoException e) {  
            e.printStackTrace();  
        }  
  
    }  

/** 
     * 資料刪除 
     */  
    private static void deleteColData(DBCollection table,String name){ 
    BasicDBObject query = new BasicDBObject();  
    query.put("name", name); 
    table.remove(query); 
    }  
}