日期:2014-05-16 浏览次数:20689 次
/*Drop Collections if exist */ db.menu.drop(); /* Initial Menu */ db.menu.insert({'name':'Roles','age':'12', 'url':'/account/getRoleList.do', 'group':'Account Managment','sequence':'01-01'}); db.menu.insert({'name':'Users','age':'22', 'url':'/account/getAccountList.do', 'group':'Account Managment','sequence':'01-02'}); db.menu.insert({'name':'Message Sending', 'age':'33','url':'/message/showSendMessage.do', 'group':'Messaging','sequence':'02-01'}); db.menu.insert({'name':'Message List', 'age':'15','url':'/message/messageList.do', 'group':'Messaging','sequence':'02-02'});
package com.cpkf.dao.mongo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.regex.Pattern; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; public class MongoDbTest { public static void test(){ Mongo mongo = null; DB db = null; InputStream is = null; BufferedReader br = null; StringBuffer sb = new StringBuffer(); String temp = ""; DBCursor dbCursor = null; try { //创建数据库连接 mongo = new Mongo("localhost",27017); db = mongo.getDB("test"); //添加用户并验证用户权限 char[] pwd = {'r','o','o','t'}; db.addUser("root",pwd); boolean flag = db.authenticate("root", pwd); System.out.println(flag); System.out.println("=================="); //初始化数据库 // is = MongoDbTest.class.getResourceAsStream("/conf/test.sql"); // br = new BufferedReader(new InputStreamReader(is,"utf-8")); // while((temp = br.readLine()) != null){ // sb.append(temp); // } // db.eval(sb.toString()); //查看数据库列表 for(String s : mongo.getDatabaseNames()){ System.out.println(s); } System.out.println("=================="); //删除数据库 mongo.dropDatabase("admin"); //查看当前库下数据集列表 Set<String> collections = db.getCollectionNames(); for(String coll : collections){ System.out.println(coll); } System.out.println("=================="); //得到一个数据集的索引列表 DBCollection menuCollection = db.getCollection("menu"); List<DBObject> indexList = menuCollection.getIndexInfo(); for(DBObject object : indexList){ System.out.println(object); } System.out.println("=================="); //查询数据集第一条记录 DBObject firstDbObject = menuCollection.findOne(); System.out.println(firstDbObject); System.out.println("=================="); //查询数据集所有记录 List<DBObject> menuList = menuCollection.find().toArray(); for(DBObject o : menuList){ System.out.println(o); } System.out.println("=================="); //以游标的形式 dbCursor = menuCollection.find(); while(dbCursor.hasNext()){ System.out.println(dbCursor.next()); } System.out.println("=================="); //查询数据量 System.out.println(menuCollection.find().count()); System.out.println(menuCollection.find(new BasicDBObject("name","Roles")).count()); System.out.println("=================="); //带条件查询 BasicDBObject bo = new BasicDBObject(); bo.put("name", "Roles"); dbCursor = menuCollection.find(bo); while(dbCursor.hasNext()){ System.out.println(dbCursor.next()); } System.out.println("=================="); //分页查询-skip-跳过几条数据-limit-限制几条数据 dbCursor = menuCollection.find().skip(2).limit(2); while(dbCursor.hasNext()){ System.out.println(dbCursor.next()); } System.out.println("=================="); //比较查询"$gt": 大于 "$gte":大于等于 "$lt": 小于 "$lte":小于等于 "$in": 包含 (支持数字比较,不支持字符串比较) BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("age", new Bas