详解MongoDB数据库基础操作及实例

栏目: 数据库 · MongoDB · 发布时间: 7年前

内容简介:这篇文章主要介绍了详解MongoDB数据库基础操作及实例的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

详解数据库基础操作及实例

          废话不多说,直接上代码,注释写的比较清楚,大家参考下,

 示例代码:

/** 
 * 插入一条DB对象 
 */ 
public static void addDBObject(DBCollection collection,BasicDBObject object){ 
  collection.insert(object); 
} 
 
/** 
 * 根据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; 
  } 
} 
 
/** 
 * 根据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; 
  } 
} 
 
/** 
 * 根据数据库名获取(新增)下面所有聚集名(表名) 
 */ 
public static Set<String> getCollectionsNames(String DBName) throws MongoException, UnknownHostException{ 
  return getDB(DBName).getCollectionNames(); 
} 
 
/** 
 * 遍历聚集中的db对象集合(相当于关系数据库中的数据) 
 */ 
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; 
} 
 
/** 
 * 获取/新增聚集(相当于关系数据库表) 
 */ 
public static DBCollection getDBCollection(String DBName,String collectionName) throws UnknownHostException, MongoException{ 
  return getDB(DBName).getCollection(collectionName); 
} 
 
/** 
 * 获取/新增数据库 
 */ 
public static DB getDB(String DBName) throws UnknownHostException, MongoException{ 
  return getMongo().getDB(DBName); 
} 
 
/** 
 * 连接数据库 
 */ 
public static Mongo getMongo() throws UnknownHostException, MongoException{ 
  Mongo mg = null; 
  if(mg == null){ 
    mg = new Mongo(); 
  } 
  return mg; 
} 
 
/** 
 * 关闭连接 
 */ 
public static void destory(Mongo mg) { 
  if (mg != null){ 
    mg.close(); 
    mg = null;  
  } 
  System.gc();   
} 
 
/** 
 * 获取数据库名 
 */ 
public static List<String> getDBNames() throws MongoException, UnknownHostException{ 
  return getMongo().getDatabaseNames(); 
} 
 
/** 
 * 删除数据库 
 */ 
public static void deleteDB(String DBName) throws MongoException, UnknownHostException{ 
  getMongo().dropDatabase(DBName); 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning XML with DOM and Ajax

Beginning XML with DOM and Ajax

Sas Jacobs / Apress / 2006-06-05 / USD 39.99

Don't waste time on 1,000-page tomes full of syntax; this book is all you need to get ahead in XML development. Renowned web developer Sas Jacobs presents an essential guide to XML. Beginning XML with......一起来看看 《Beginning XML with DOM and Ajax》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试