Detail MongoDB database operations and examples

  • 2020-06-19 11:57:38
  • OfStack

Detailed database operations and examples

No more nonsense, directly on the code, comments written more clearly, you can refer to,

Sample code:


/** 
 *  insert 1 article DB object  
 */ 
public static void addDBObject(DBCollection collection,BasicDBObject object){ 
  collection.insert(object); 
} 
 
/** 
 *  According to the id The query 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; 
  } 
} 
 
/** 
 *  According to the key and value Values for the query  
 */ 
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; 
  } 
} 
 
/** 
 *  Based on the database name ( new ) All the following cluster names (table names)  
 */ 
public static Set<String> getCollectionsNames(String DBName) throws MongoException, UnknownHostException{ 
  return getDB(DBName).getCollectionNames(); 
} 
 
/** 
 *  Iterate through the collections db A collection of objects ( Equivalent to data in a relational database ) 
 */ 
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; 
} 
 
/** 
 *  To obtain / New aggregation (equivalent to relational database tables)  
 */ 
public static DBCollection getDBCollection(String DBName,String collectionName) throws UnknownHostException, MongoException{ 
  return getDB(DBName).getCollection(collectionName); 
} 
 
/** 
 *  To obtain / New database  
 */ 
public static DB getDB(String DBName) throws UnknownHostException, MongoException{ 
  return getMongo().getDB(DBName); 
} 
 
/** 
 *  Connect to database  
 */ 
public static Mongo getMongo() throws UnknownHostException, MongoException{ 
  Mongo mg = null; 
  if(mg == null){ 
    mg = new Mongo(); 
  } 
  return mg; 
} 
 
/** 
 *  Close the connection  
 */ 
public static void destory(Mongo mg) { 
  if (mg != null){ 
    mg.close(); 
    mg = null;  
  } 
  System.gc();   
} 
 
/** 
 *  Get the database name  
 */ 
public static List<String> getDBNames() throws MongoException, UnknownHostException{ 
  return getMongo().getDatabaseNames(); 
} 
 
/** 
 *  Delete database  
 */ 
public static void deleteDB(String DBName) throws MongoException, UnknownHostException{ 
  getMongo().dropDatabase(DBName); 
} 

If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, hope to help you, thank you for your support to this site!


Related articles: