test.sql
/*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'});
MongoDbTest
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 BasicDBObject("$gt",20).append("$lt", 30));
dbCursor = menuCollection.find(basicDBObject);
while(dbCursor.hasNext()){
System.out.println(dbCursor.next());
}
System.out.println("==================");
//插入数据
List<DBObject> list = new ArrayList<DBObject>();
for(int i = 0;i < 40;i ++){
BasicDBObject dbs = new BasicDBObject();
dbs.put("name", "hj" + i);
dbs.put("age", i);
BasicDBObject info = new BasicDBObject();
info.put("x", i);
info.put("y", i + 1);
dbs.put("info", info);
list.add(dbs);
}
//创建数据集,该语句重复执行抛出异常
// db.createCollection("infoList",list.get(0));
DBCollection infoCollection = db.getCollection("infoList");
// infoCollection.insert(list);
dbCursor = infoCollection.find();
while(dbCursor.hasNext()){
System.out.println(dbCursor.next());
}
System.out.println("==================");
//正则表达式匹配查询CASE_INSENSITIVE-不区分大小写的匹配
Pattern pattern = Pattern.compile("us",Pattern.CASE_INSENSITIVE);
BasicDBObject pattDB = new BasicDBObject("name",pattern);
dbCursor = menuCollection.find(pattDB);
while(dbCursor.hasNext()){
System.out.println(dbCursor.next());
}
System.out.println("==================");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MongoDbTest.test();
}
}