您的位置:首页 > 其它

mongdb模糊查询与分页

2016-12-29 17:49 253 查看
http://www.jb51.net/article/83278.htm

本文实例为大家分享了Java操作MongoDB模糊查询和分页查询,供大家参考,具体内容如下

模糊查询条件:

1、完全匹配

Pattern pattern = Pattern.compile("^name$", Pattern.CASE_INSENSITIVE);

2、右匹配

Pattern pattern = Pattern.compile("^.*name$", Pattern.CASE_INSENSITIVE);

3、左匹配

Pattern pattern = Pattern.compile("^name.*$", Pattern.CASE_INSENSITIVE);

4、模糊匹配

Pattern pattern = Pattern.compile("^.*name8.*$", Pattern.CASE_INSENSITIVE);
记录总数查询:

count(),返回查询总数。
查询记录排序:
BasicDBObject sort = new BasicDBObject();

sort.put("name",1);

1、表示正序;-1.表示倒序
分页查询:
skip(),跳过多少条记录

limit(),返回多少条记录

packagecom.what21.mongodb.demo;

importjava.util.ArrayList;
importjava.util.List;
importjava.util.Set;
importjava.util.regex.Pattern;

importcom.mongodb.BasicDBObject;
importcom.mongodb.DB;
importcom.mongodb.DBCollection;
importcom.mongodb.DBCursor;
importcom.mongodb.DBObject;
importcom.mongodb.MongoClient;
importcom.mongodb.MongoClientOptions;
importcom.mongodb.MongoCredential;
importcom.mongodb.ServerAddress;

publicclass OperateDemo2 {

/**
* @return
* @throws Exception
*/
publicstatic MongoClient getMongoClient()throwsException{
try{
//===================================================//
List<ServerAddress> serverList = newArrayList<ServerAddress>();
serverList.add(newServerAddress("192.168.18.85",27017));
//===================================================//
List<MongoCredential> mcList = newArrayList<MongoCredential>();
String username = "root";
String database = "demo";
char[] password = "root123".toCharArray();
mcList.add(MongoCredential.createCredential(username, database,password));
//===================================================//
MongoClientOptions.Builder builder = MongoClientOptions.builder();
// 与目标数据库能够建立的最大connection数量为50
builder.connectionsPerHost(50);
// 如果当前所有的connection都在使用中,则每个connection上可以有50个线程排队等待
builder.threadsAllowedToBlockForConnectionMultiplier(50);
// 一个线程访问数据库的时候,在成功获取到一个可用数据库连接之前的最长等待时间为2分钟
// 这里比较危险,如果超过maxWaitTime都没有获取到这个连接的话,该线程就会抛出Exception
// 故这里设置的maxWaitTime应该足够大,以免由于排队线程过多造成的数据库访问失败
builder.maxWaitTime(1000*60*2);
// 与数据库建立连接的timeout设置为1分钟
builder.connectTimeout(1000*60*1);
//===================================================//
MongoClientOptions mco = builder.build();
returnnew MongoClient(serverList, mcList, mco);
}catch(Exception e) {
throwe;
}
}

/**
* @param dbname
* @return
* @throws Exception
*/
publicstatic DB getDB(String dbname) throwsException{
returngetMongoClient().getDB(dbname);
}

/**
* @param db
*/
publicstatic void collections(DB db){
Set<String> colls = db.getCollectionNames();
for(String collName : colls) {
System.out.println(collName);
}
}

/**
* 记录总数查询
*
* @param db
* @param name
*/
publicstatic void count(DB db,String name){
DBCollection dbColl = db.getCollection(name);
intcount = dbColl.find().count();
System.out.println("共有: " + count + "个");
}

/**
* 模糊查询
*
* @param db
* @param name
*/
publicstatic void query(DB db,String name){
DBCollection dbColl = db.getCollection(name);
//完全匹配
//Pattern pattern = Pattern.compile("^name$", Pattern.CASE_INSENSITIVE);
//右匹配
//Pattern pattern = Pattern.compile("^.*name$", Pattern.CASE_INSENSITIVE);
//左匹配
//Pattern pattern = Pattern.compile("^name.*$", Pattern.CASE_INSENSITIVE);
//模糊匹配
Pattern pattern = Pattern.compile("^.*name8.*$", Pattern.CASE_INSENSITIVE);
BasicDBObject query = newBasicDBObject();
query.put("name",pattern);
BasicDBObject sort = newBasicDBObject();
// 1,表示正序; -1,表示倒序
sort.put("name",1);
DBCursor cur = dbColl.find(query).sort(sort);
intcount = 0;
while(cur.hasNext()) {
DBObject obj = cur.next();
System.out.print("name="+ obj.get("name"));
System.out.print(",email="+ obj.get("email"));
System.out.println(",passwd="+ obj.get("passwd"));
count ++;
}
System.out.println("共有: " + count + "个");
}

/**
* 分页查询
*
* @param db
* @param name
* @param start
* @param pageSize
*/
publicstatic void page(DB db,String name,intstart,intpageSize){
DBCollection dbColl = db.getCollection(name);
BasicDBObject sort = newBasicDBObject();
sort.put("name",1);
DBCursor cur = dbColl.find().sort(sort).skip(start).limit(pageSize);;
intcount = 0;
while(cur.hasNext()) {
DBObject obj = cur.next();
System.out.print("name="+ obj.get("name"));
System.out.print(",email="+ obj.get("email"));
System.out.println(",passwd="+ obj.get("passwd"));
count ++;
}
System.out.println("共有: " + count + "个");
}
/**
* @param args
* @throws Exception
*/
publicstatic void main(String[] args) throwsException {
DB db = getDB("demo");
collections(db);
String name = "users";
System.out.println("count()=================================================");
count(db,name);
System.out.println("query()=================================================");
query(db,name);
System.out.println("page()=================================================");
page(db,name,10,10);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: