1
1 package mongodb; 2 3 import java.net.UnknownHostException; 4 import java.util.ArrayList; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Set; 8 9 import com.mongodb.BasicDBObject; 10 import com.mongodb.DB; 11 import com.mongodb.DBCollection; 12 import com.mongodb.DBCursor; 13 import com.mongodb.DBObject; 14 import com.mongodb.Mongo; 15 import com.mongodb.MongoException; 16 17 /** 18 * java 19 * mongodb的数据插入、读取、更新、删除 20 */ 21 public class MongodbCRUD { 22 private static Mongo m = null; 23 private static DB db = null; 24 25 //数据集合名称 26 private static final String COLLECTION_NAME = "mcpang"; 27 28 /* 29 * 测试java处理mongodb的增、删、改、查操作 30 */ 31 public static void main(String[] args) { 32 //获取数据库连接 33 startMongoDBConn(); 34 //保存数据 35 createColData(); 36 //读取数据 37 readColData(); 38 //更新数据 39 updateColData(); 40 //读取数据 41 readColData(); 42 //删除数据 43 deleteColData(); 44 //读取数据 45 readColData(); 46 //删除数据集 47 db.getCollection(COLLECTION_NAME).drop(); 48 //关闭数据库连接 49 stopMondoDBConn(); 50 51 52 } 53 54 /** 55 * 数据插入 56 * *测试数据: 57 * 【name:小李、age:30、address:江苏南京】 58 * 【name:小张、age:25、address:江苏苏州】 59 * @return 60 */ 61 private static void createColData(){ 62 DBCollection dbCol = db.getCollection(COLLECTION_NAME); 63 System.out.println("向数据集中插入数据开始:"); 64 List<DBObject> dbList = new ArrayList<DBObject>(); 65 BasicDBObject doc1 = new BasicDBObject(); 66 doc1.put("name", "小李"); 67 doc1.put("age", 30); 68 doc1.put("address", "江苏南京"); 69 dbList.add(doc1); 70 71 BasicDBObject doc2 = new BasicDBObject(); 72 doc2.put("name", "小张"); 73 doc2.put("age", 25); 74 doc2.put("address", "江苏苏州"); 75 dbList.add(doc2); 76 77 dbCol.insert(dbList); 78 System.out.println("向数据集中插入数据完成!"); 79 System.out.println("------------------------------"); 80 } 81 82 /** 83 * 数据读取 84 */ 85 private static void readColData(){ 86 DBCollection dbCol = db.getCollection(COLLECTION_NAME); 87 DBCursor ret = dbCol.find(); 88 System.out.println("从数据集中读取数据:"); 89 while(ret.hasNext()){ 90 BasicDBObject bdbObj = (BasicDBObject) ret.next(); 91 if(bdbObj != null){ 92 System.out.println("name:"+bdbObj.getString("name")); 93 System.out.println("age:"+bdbObj.getInt("age")); 94 System.out.println("address:"+bdbObj.getString("address")); 95 } 96 } 97 } 98 99 /** 100 * 数据更新 101 * update(q, o, upsert, multi) 102 * update(q, o, upsert, multi, concern) 103 * update(arg0, arg1, arg2, arg3, arg4, arg5) 104 * updateMulti(q, o) 105 */ 106 private static void updateColData(){ 107 System.out.println("------------------------------"); 108 DBCollection dbCol = db.getCollection(COLLECTION_NAME); 109 DBCursor ret = dbCol.find(); 110 BasicDBObject doc = new BasicDBObject(); 111 BasicDBObject res = new BasicDBObject(); 112 res.put("age", 40); 113 System.out.println("将数据集中的所有文档的age修改成40!"); 114 doc.put("$set", res); 115 dbCol.update(new BasicDBObject(),doc,false,true); 116 System.out.println("更新数据完成!"); 117 System.out.println("------------------------------"); 118 } 119 120 /** 121 * 数据删除 122 */ 123 private static void deleteColData(){ 124 System.out.println("------------------------------"); 125 DBCollection dbCol = db.getCollection(COLLECTION_NAME); 126 System.out.println("删除【小李】!"); 127 BasicDBObject doc = new BasicDBObject(); 128 doc.put("name", "小李"); 129 dbCol.remove(doc); 130 System.out.println("------------------------------"); 131 } 132 133 /** 134 * 关闭mongodb数据库连接 135 */ 136 private static void stopMondoDBConn(){ 137 if (null != m) { 138 if (null != db) { 139 // 结束Mongo数据库的事务请求 140 try { 141 db.requestDone(); 142 } catch(Exception e) { 143 e.printStackTrace(); 144 } 145 } 146 try 147 { 148 m.close(); 149 } catch(Exception e) { 150 e.printStackTrace(); 151 } 152 m = null; 153 db = null; 154 } 155 } 156 157 /** 158 * 获取mongodb数据库连接 159 */ 160 private static void startMongoDBConn(){ 161 try { 162 //Mongo(p1, p2):p1=>IP地址 p2=>端口 163 m = new Mongo("127.0.0.1", 27017); 164 //根据mongodb数据库的名称获取mongodb对象 165 db = m.getDB("sdap"); 166 //校验用户密码是否正确 167 if (!db.authenticate("sdap", "sdap123".toCharArray())){ 168 System.out.println("连接MongoDB数据库,校验失败!"); 169 }else{ 170 System.out.println("连接MongoDB数据库,校验成功!"); 171 } 172 } catch (UnknownHostException e) { 173 e.printStackTrace(); 174 } catch (MongoException e) { 175 e.printStackTrace(); 176 } 177 } 178 179 }