1. 程式人生 > >mongodb 筆記

mongodb 筆記

user net .get 集合 display mongod person 更新數據 mov

1.java驅動下的mongo操作

  1.1 數據庫連接

  

技術分享圖片
 1 package com.mongodb.text;
 2 
 3 import java.net.UnknownHostException;
 4 
 5 import com.mongodb.DB;
 6 import com.mongodb.DBCollection;
 7 import com.mongodb.DBCursor;
 8 import com.mongodb.DBObject;
 9 import com.mongodb.Mongo;
10 import com.mongodb.MongoException;
11 import com.mongodb.util.JSON; 12 13 public class DataBase { 14 public static void main(String[] args) 15 throws UnknownHostException, MongoException { 16 //1.建立一個Mongo的數據庫連接對象 17 Mongo mg = new Mongo("127.0.0.1:27017"); 18 //查詢所有的Database 19 for (String name : mg.getDatabaseNames()) {
20 System.out.println("dbName: " + name); 21 } 22 //2.創建相關數據庫的連接 23 DB db = mg.getDB("foobar"); 24 //查詢數據庫所有的集合 25 for (String name : db.getCollectionNames()) { 26 System.out.println("collectionName: " + name); 27 } 28 29 DBCollection users = db.getCollection("persons");
30 //查詢所有的數據 31 DBCursor cur = users.find(); 32 while (cur.hasNext()) { 33 DBObject object = cur.next(); 34 System.out.println(object.get("name")); 35 } 36 System.out.println(cur.count()); 37 System.out.println(cur.getCursorId()); 38 System.out.println(JSON.serialize(cur)); 39 } 40 }
DataBase

1.2 數據庫常見操作

技術分享圖片
  1 package com.mongodb.text;
  2 
  3 import java.net.UnknownHostException;
  4 import java.util.List;
  5 
  6 import org.bson.types.ObjectId;
  7 
  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 public class MongoDb {
 17     //1.建立一個Mongo的數據庫連接對象
 18     static Mongo connection = null;
 19     //2.創建相關數據庫的連接
 20     static DB db = null;
 21     public MongoDb(String dbName) throws UnknownHostException, MongoException{
 22         connection = new Mongo("127.0.0.1:27017");
 23         db = connection.getDB(dbName);
 24     }
 25     public static void main(String[] args) throws UnknownHostException, MongoException {
 26         //實例化
 27         MongoDb mongoDb = new MongoDb("foobar");
 28         /**
 29          * 1.創建一個名字叫javadb的數據庫
 30          */
 31 //        mongoDb.createCollection("javadb");
 32         /**
 33          * 2.為集合javadb添加一條數據
 34          */
 35 //        DBObject dbs = new BasicDBObject();
 36 //        dbs.put("name", "uspcat.com");
 37 //        dbs.put("age", 2);
 38 //        List<String>  books = new ArrayList<String>();
 39 //        books.add("EXTJS");
 40 //        books.add("MONGODB");
 41 //        dbs.put("books", books);
 42 //        mongoDb.insert(dbs, "javadb");
 43         /**
 44          * 3.批量插入數據
 45          */
 46 //        List<DBObject> dbObjects = new ArrayList<DBObject>();
 47 //        DBObject jim = new BasicDBObject("name","jim");
 48 //        DBObject lisi = new BasicDBObject("name","lisi");
 49 //        dbObjects.add(jim);
 50 //        dbObjects.add(lisi);
 51 //        mongoDb.insertBatch(dbObjects, "javadb");
 52         /**
 53          * 4.根據ID刪除數據
 54          */
 55 //        mongoDb.deleteById("502870dab9c368bf5b151a04", "javadb");
 56         /**
 57          * 5.根據條件刪除數據
 58          */
 59 //        DBObject lisi = new BasicDBObject();
 60 //        lisi.put("name", "lisi");
 61 //        int count = mongoDb.deleteByDbs(lisi, "javadb");
 62 //        System.out.println("刪除數據的條數是: "+count);
 63         /**
 64          * 6.更新操作,為集合增加email屬性
 65          */
 66 //        DBObject update = new BasicDBObject();
 67 //        update.put("$set", 
 68 //                new BasicDBObject("eamil","[email protected]"));
 69 //        mongoDb.update(new BasicDBObject(),
 70 //                update,false,true,"javadb");
 71         /**
 72          * 7.查詢出persons集合中的name和age
 73          */
 74 //        DBObject keys = new BasicDBObject();
 75 //        keys.put("_id", false);
 76 //        keys.put("name", true);
 77 //        keys.put("age", true);
 78 //        DBCursor cursor = mongoDb.find(null, keys, "persons");
 79 //        while (cursor.hasNext()) {
 80 //            DBObject object = cursor.next();
 81 //            System.out.println(object.get("name"));
 82 //        }
 83         /**
 84          * 8.查詢出年齡大於26歲並且英語成績小於80分
 85          */
 86 //        DBObject ref = new BasicDBObject();
 87 //        ref.put("age", new BasicDBObject("$gte",26));
 88 //        ref.put("e", new BasicDBObject("$lte",80));
 89 //        DBCursor cursor = mongoDb.find(ref, null, "persons");
 90 //        while (cursor.hasNext()) {
 91 //            DBObject object = cursor.next();
 92 //            System.out.print(object.get("name")+"-->");
 93 //            System.out.print(object.get("age")+"-->");
 94 //            System.out.println(object.get("e"));
 95 //        }
 96         /**
 97          * 9.分頁例子
 98          */
 99         DBCursor cursor = mongoDb.find(null, null, 0, 3, "persons");
100         while (cursor.hasNext()) {
101             DBObject object = cursor.next();
102             System.out.print(object.get("name")+"-->");
103             System.out.print(object.get("age")+"-->");
104             System.out.println(object.get("e"));
105         }        
106         //關閉連接對象
107         connection.close();
108     }
109     /**
110      * 穿件一個數據庫集合
111      * @param collName 集合名稱
112      * @param db  數據庫實例
113      */
114     public void createCollection(String collName){
115         DBObject dbs = new BasicDBObject();
116         db.createCollection("javadb", dbs);
117     }
118     /**
119      * 為相應的集合添加數據
120      * @param dbs
121      * @param collName
122      */
123     public void insert(DBObject dbs,String collName){
124         //1.得到集合
125         DBCollection coll = db.getCollection(collName);
126         //2.插入操作
127         coll.insert(dbs);
128     }
129     /**
130      * 為集合批量插入數據
131      * @param dbses
132      * @param collName
133      */
134     public void insertBatch(List<DBObject> dbses,String collName){
135         //1.得到集合
136         DBCollection coll = db.getCollection(collName);
137         //2.插入操作
138         coll.insert(dbses);
139     }
140     /**
141      * 根據id刪除數據
142      * @param id
143      * @param collName
144      * @return 返回影響的數據條數
145      */
146     public int deleteById(String id,String collName){
147         //1.得到集合
148         DBCollection coll = db.getCollection(collName);
149         DBObject dbs = new BasicDBObject("_id", new ObjectId(id));
150         int count = coll.remove(dbs).getN();
151         return count;
152     }
153     /**
154      * 根據條件刪除數據
155      * @param id
156      * @param collName
157      * @return 返回影響的數據條數
158      */    
159     public int deleteByDbs(DBObject dbs,String collName){
160         //1.得到集合
161         DBCollection coll = db.getCollection(collName);
162         int count = coll.remove(dbs).getN();
163         return count;
164     }
165     /**
166      * 更新數據
167      * @param find 查詢器
168      * @param update 更新器
169      * @param upsert 更新或插入
170      * @param multi 是否批量更新
171      * @param collName 集合名稱
172      * @return 返回影響的數據條數
173      */
174     public int update(DBObject find,
175                         DBObject update,
176                         boolean upsert,
177                         boolean multi,
178                         String collName){
179         //1.得到集合
180         DBCollection coll = db.getCollection(collName);
181         int count = coll.update(find, update, upsert, multi).getN();
182         return count;
183     }
184     /**
185      * 查詢器(分頁)
186      * @param ref
187      * @param keys
188      * @param start
189      * @param limit
190      * @return
191      */
192     public DBCursor find(DBObject ref, 
193             DBObject keys,
194             int start,
195             int limit,
196             String collName){
197         DBCursor cur = find(ref, keys, collName);
198         return cur.limit(limit).skip(start);
199     }
200     /**
201      * 查詢器(不分頁)
202      * @param ref
203      * @param keys
204      * @param start
205      * @param limit
206      * @param collName
207      * @return
208      */
209     public DBCursor find(DBObject ref,
210             DBObject keys,
211             String collName){
212         //1.得到集合
213         DBCollection coll = db.getCollection(collName);
214         DBCursor cur = coll.find(ref, keys);
215         return cur;
216     }    
217 }
MongoDb

mongodb 筆記