国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

詳解MongoDB數(shù)據(jù)庫基礎(chǔ)操作及實例

2020-03-14 12:51:52
字體:
供稿:網(wǎng)友

詳解數(shù)據(jù)庫基礎(chǔ)操作及實例

          廢話不多說,直接上代碼,注釋寫的比較清楚,大家參考下,

 示例代碼:

/**  * 插入一條DB對象  */ public static void addDBObject(DBCollection collection,BasicDBObject object){   collection.insert(object); }  /**  * 根據(jù)id查詢DBObject  */ public static DBObject getDBObjectById(String value) throws UnknownHostException, MongoException{   dbc = getDBCollection("company", "users").find(new BasicDBObject("_id",new ObjectId(value)));   DBObject ob = null;   int i = 0;   while(dbc.hasNext()){     ob = dbc.next();     i++;   }   if(i == 1){     return ob;   }else{     return null;   } }  /**  * 根據(jù)key和value值查詢  */ public static DBObject getDBObject(String key,String value) throws UnknownHostException, MongoException{   dbc = getDBCollection("company", "users").find(new BasicDBObject(key,value));   DBObject ob = null;   int i = 0;   while(dbc.hasNext()){     ob = dbc.next();     i++;   }   if(i == 1){     return ob;   }else{     return null;   } }  /**  * 根據(jù)數(shù)據(jù)庫名獲取(新增)下面所有聚集名(表名)  */ public static Set<String> getCollectionsNames(String DBName) throws MongoException, UnknownHostException{   return getDB(DBName).getCollectionNames(); }  /**  * 遍歷聚集中的db對象集合(相當(dāng)于關(guān)系數(shù)據(jù)庫中的數(shù)據(jù))  */ public static Set<DBObject> getDBObjects(DBCollection collection){   Set<DBObject> dbObjects = new HashSet<DBObject>();   DBCursor cursor = collection.find();   while(cursor.hasNext()){     DBObject object = cursor.next();     dbObjects.add(object);   }   return dbObjects; }  /**  * 獲取/新增聚集(相當(dāng)于關(guān)系數(shù)據(jù)庫表)  */ public static DBCollection getDBCollection(String DBName,String collectionName) throws UnknownHostException, MongoException{   return getDB(DBName).getCollection(collectionName); }  /**  * 獲取/新增數(shù)據(jù)庫  */ public static DB getDB(String DBName) throws UnknownHostException, MongoException{   return getMongo().getDB(DBName); }  /**  * 連接數(shù)據(jù)庫  */ public static Mongo getMongo() throws UnknownHostException, MongoException{   Mongo mg = null;   if(mg == null){     mg = new Mongo();   }   return mg; }  /**  * 關(guān)閉連接  */ public static void destory(Mongo mg) {   if (mg != null){     mg.close();     mg = null;    }   System.gc();   }  /**  * 獲取數(shù)據(jù)庫名  */ public static List<String> getDBNames() throws MongoException, UnknownHostException{   return getMongo().getDatabaseNames(); }  /**  * 刪除數(shù)據(jù)庫  */ public static void deleteDB(String DBName) throws MongoException, UnknownHostException{   getMongo().dropDatabase(DBName); } 

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關(guān)教程知識閱讀請移步到MongoDB頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 城市| 大同市| 神木县| 富宁县| 工布江达县| 武定县| 阳谷县| 永定县| 盐源县| 康乐县| 伊春市| 无为县| 兰西县| 长子县| 普宁市| 黄浦区| 九江市| 尚志市| 兴安盟| 两当县| 洞头县| 崇州市| 三明市| 忻城县| 乌兰浩特市| 报价| 清流县| 青阳县| 宝丰县| 麻城市| 中牟县| 长垣县| 闵行区| 绥江县| 九江市| 通海县| 额敏县| 葫芦岛市| 商水县| 汉源县| 禄丰县|