public class MongoManager {
private static final String MONGO_DBNAME="local"; private static final String COLLECTION_NAME="person"; private Mongo mongo=null; private DB db=null; private DBCollection coll=null; public MongoManager(){ try { mongo=new Mongo(); db=mongo.getDB(MONGO_DBNAME); coll=db.getCollection(COLLECTION_NAME); System.out.println("db:"+db+",coll:"+coll); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MongoException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void destory(){ if(mongo !=null){ mongo.close(); mongo=null; db=null; } } //查询单个与查询多个. public void queryAll(){ System.out.println("queryAll start..."); DBCursor cursor = coll.find(); while(cursor.hasNext()){ System.out.println("queryAll:"+cursor.next()); } } //单条插入与多条插入。 public void add(){ queryAll(); System.out.println("add befor count:"+coll.count()); for(int i=0;i<5;i++){ DBObject object=new BasicDBObject(); object.put("name", "lkj"+i); object.put("age", i); object.put("sex", "男"); coll.save(object); } System.out.println("add after count:"+coll.count()); } //单条更新与多条更新。 public void update(){ //int i=coll.update(new BasicDBObject("_id",new ObjectId("5680d3844b3ad1cd62dc8a82")), new BasicDBObject("age","22")).getN(); //System.out.println("update i:"+i);// int j=coll.update(new BasicDBObject("_id", new ObjectId("5680d3844b3ad1cd62dc8a82")), new BasicDBObject("age", "23"), true, false).getN();// System.out.println("update j:"+j); BasicDBObject db=new BasicDBObject("name", "lkj"); db.append("age", 21); db.append("sex", "男"); BasicDBObject db2=new BasicDBObject("$set", db); BasicDBObject db3=new BasicDBObject(); Pattern p=Pattern.compile("^lkp2.*$", Pattern.CASE_INSENSITIVE); db3.append("name", p); //int k=coll.update(new BasicDBObject("_id", new ObjectId("5680d3844b3ad1cd62dc8a83")), db2).getN(); //前面是满足条件的,后面是要更新的内容. int k=coll.updateMulti(db3, db2).getN(); System.out.println("update k:"+k); } //单条删除与多条删除 public void delete(){ //int i=coll.remove(new BasicDBObject("_id",new ObjectId("5680d3844b3ad1cd62dc8a82"))).getN(); //System.out.println("delete i:"+i); BasicDBObject db3=new BasicDBObject(); Pattern p=Pattern.compile("^lkj.*$", Pattern.CASE_INSENSITIVE); db3.append("name", p); BasicDBList dblist=new BasicDBList(); dblist.add("lkj0"); dblist.add("lkj1"); BasicDBObject db=new BasicDBObject("name", new BasicDBObject("$in", dblist)); int k=coll.remove(db).getN(); System.out.println("delete k:"+k); } //查询单个.多个条件.模糊查询. public void querySingle(){ // BasicDBObject db=new BasicDBObject("_id", new ObjectId("5680d2cb4b3ae0e80dc4cd16"));// BasicDBObject db2=new BasicDBObject("age", new BasicDBObject("$lte", 26));// db2.append("name", "lkj");// db2.append("sex", "男");// BasicDBList dblist=new BasicDBList();// dblist.add(2);// dblist.add(3);// dblist.add(4); BasicDBObject db=new BasicDBObject();// db.put("age", new BasicDBObject("$in", dblist)); Pattern p=Pattern.compile("^.*1$", Pattern.CASE_INSENSITIVE); db.append("name", p); DBCursor cursor = coll.find(db); while(cursor.hasNext()){ System.out.println("querySingle:"+cursor.next()); } } public static void main(String[] args) { MongoManager mm=new MongoManager(); //mm.add(); //mm.update(); // mm.delete(); mm.queryAll(); //mm.querySingle(); }}