当前位置:数据库 > mongodb >>

NoSql之MongoDB的常用类管理

NoSql之MongoDB的常用类管理
 
1、DBCollection的管理  www.zzzyk.com  
[java] 
package com.boonya.mongo;  
import java.util.Set;  
import com.mongodb.DBCollection;  
public class CollectionManager {  
   
 public static DBCollection  getDbCollection(String collectionName){  
  return DBConnection.getInstance().getCollection(collectionName.equals("")?"myCollection":collectionName);  
 }  
   
 public String getCollectionNames(){  
  String strNames="";  
  Set<String> colls =DBConnection.getInstance().getCollectionNames();  
  for (String cname : colls) {  
   strNames+=cname+",";  
  }  
  return strNames;  
 }  
  
}  
2、并发操作管理
[java] 
package com.boonya.mongo;  
import com.mongodb.BasicDBObject;  
import com.mongodb.DB;  
import com.mongodb.DBCollection;  
import com.mongodb.DBObject;  
import com.mongodb.WriteConcern;  
  
public class ConcurrencyManager {  
   
 private static DBCollection coll= CollectionManager.getDbCollection("myCollection");  
   
 /** 
  * If you want to ensure complete consistency in a “session” (maybe an http request),  
  * you would want the driver to use the same socket, which you can achieve by using a 
  * “consistent request”. Call requestStart() before your operations and requestDone() 
  * to release the connection back to the pool: 
  * DB and DBCollection are completely thread safe. In fact, they are cached so you get 
  * the same instance no matter what 
  */  
 public void insertData(){  
  DB db=DBConnection.getInstance();  
  db.requestStart();  
  try {  
   db.requestEnsureConnection();  
   //do something  
  } finally {  
   db.requestDone();  
  }  
  
 }  
   
 /** 
  * Since by default a connection is given back to the pool after each request, you may  
  * wonder how calling getLastError() works after a write. You should actually use a  
  * write concern like WriteConcern.SAFE instead of calling getLastError() manually.  
  * The driver will then call getLastError() before putting the connection back in the pool. 
  *  
  * WriteConcern.NONE            No exceptions thrown.  
     * WriteConcern.NORMAL          Exceptions are only thrown when the primary node is unreachable for 
     *                              a read, or the full replica set is unreachable.  
     * WriteConcern.SAFE            Same as the above, but exceptions thrown when there is a server error 
     *                              on writes or reads. Calls getLastError().  
     * WriteConcern.REPLICAS_SAFE   Tries to write to two separate nodes. Same as the above, but 
     *                              will throw an exception if two writes are not possible.  
     * WriteConcern.FSYNC_SAFE      Same  as WriteConcern.SAFE, but also waits for write to be written to disk.  
     * 
  */  
 public void writeConcern(){  
  coll.insert(new BasicDBObject("name","boonya").append("age", 21), WriteConcern.SAFE);  
  
  // is equivalent to  
  
  DB db=DBConnection.getInstance();  
  DBCollection coll=CollectionManager.getDbCollection("myCollection");  
  db.requestStart();  
  try {  
     coll.insert(new BasicDBObject("name","boonya").append("age", 21));  
     DBObject err = db.getLastError();  
     System.out.println(err);  
  } finally {  
     db.requestDone();  
  }  
  
 }  
  
}  
3、数据库管理
[java] 
package com.boonya.mongo;  
import java.net.UnknownHostException;  
import com.mongodb.MongoClient;  
public class DatabaseManager {  
   
 public String getDatabaseNames() throws UnknownHostException{  
  String strNames="";  
  MongoClient mongoClient;  
  mongoClient = new MongoClient();  
  for (String name : mongoClient.getDatabaseNames()) {  
     strNames+=name+",";  
  }  
  return strNames;  
 }  
   
 public void  dropDatabase(String databaseName) throws UnknownHostException{  
      MongoClient mongoClient = new MongoClient();  
      mongoClient.dropDatabase(databaseName);  
 }  
  
}  
4、文档管理
[java] 
package com.boonya.mongo;  
import java.util.ArrayList;  
import java.util.List;  
import com.mongodb.BasicDBObject;  
import com.mongodb.DBCollection;  
import com.mongodb.DBCursor;  
import com.mongodb.DBObject;  
public class DocumentManager {  
   
 private static DBCollection coll= CollectionManager.getDbCollection("myCollection");  
   
 public void insertADocument(){  
  BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database")  
   .append("count", 1)  
   .append("info", new BasicDBObject("x", 203).append("y", 102));  
         coll.insert(doc);  
 }  
   
 /** 
  * PRINT BELOW:&nbs
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,