Java操作MongoDB模糊查詢和分頁查詢
本文實例為大家分享了Java操作MongoDB模糊查詢和分頁查詢,供大家參考,具體內(nèi)容如下
模糊查詢條件:
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);
記錄總數(shù)查詢:
count(),返回查詢總數(shù)。
查詢記錄排序:
BasicDBObject sort = new BasicDBObject();
sort.put("name",1);
1、表示正序;-1.表示倒序
分頁查詢:
skip(),跳過多少條記錄
limit(),返回多少條記錄
代碼實例:
package com.what21.mongodb.demo;
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.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
public class OperateDemo2 {
/**
* @return
* @throws Exception
*/
public static MongoClient getMongoClient()throws Exception{
try {
//===================================================//
List<ServerAddress> serverList = new ArrayList<ServerAddress>();
serverList.add(new ServerAddress("192.168.18.85", 27017));
//===================================================//
List<MongoCredential> mcList = new ArrayList<MongoCredential>();
String username = "root";
String database = "demo";
char[] password = "root123".toCharArray();
mcList.add(MongoCredential.createCredential(username, database,password));
//===================================================//
MongoClientOptions.Builder builder = MongoClientOptions.builder();
// 與目標數(shù)據(jù)庫能夠建立的最大connection數(shù)量為50
builder.connectionsPerHost(50);
// 如果當前所有的connection都在使用中,則每個connection上可以有50個線程排隊等待
builder.threadsAllowedToBlockForConnectionMultiplier(50);
// 一個線程訪問數(shù)據(jù)庫的時候,在成功獲取到一個可用數(shù)據(jù)庫連接之前的最長等待時間為2分鐘
// 這里比較危險,如果超過maxWaitTime都沒有獲取到這個連接的話,該線程就會拋出Exception
// 故這里設置的maxWaitTime應該足夠大,以免由于排隊線程過多造成的數(shù)據(jù)庫訪問失敗
builder.maxWaitTime(1000*60*2);
// 與數(shù)據(jù)庫建立連接的timeout設置為1分鐘
builder.connectTimeout(1000*60*1);
//===================================================//
MongoClientOptions mco = builder.build();
return new MongoClient(serverList, mcList, mco);
} catch (Exception e) {
throw e;
}
}
/**
* @param dbname
* @return
* @throws Exception
*/
public static DB getDB(String dbname) throws Exception{
return getMongoClient().getDB(dbname);
}
/**
* @param db
*/
public static void collections(DB db){
Set<String> colls = db.getCollectionNames();
for (String collName : colls) {
System.out.println(collName);
}
}
/**
* 記錄總數(shù)查詢
*
* @param db
* @param name
*/
public static void count(DB db,String name){
DBCollection dbColl = db.getCollection(name);
int count = dbColl.find().count();
System.out.println("共有: " + count + "個");
}
/**
* 模糊查詢
*
* @param db
* @param name
*/
public static 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 = new BasicDBObject();
query.put("name",pattern);
BasicDBObject sort = new BasicDBObject();
// 1,表示正序; -1,表示倒序
sort.put("name",1);
DBCursor cur = dbColl.find(query).sort(sort);
int count = 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
*/
public static void page(DB db,String name,int start,int pageSize){
DBCollection dbColl = db.getCollection(name);
BasicDBObject sort = new BasicDBObject();
sort.put("name",1);
DBCursor cur = dbColl.find().sort(sort).skip(start).limit(pageSize);;
int count = 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
*/
public static void main(String[] args) throws Exception {
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);
}
}
以上就是Java操作MongoDB模糊查詢和分頁查詢的實現(xiàn)代碼,希望對大家的學習有所幫助。
相關文章
Security中的WebSecurityConfigurerAdapter詳解
這篇文章主要介紹了Security中的WebSecurityConfigurerAdapter詳解,今天我們要進一步的的學習如何自定義配置Spring?Security,本文結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07
spring boot 項目中使用thymeleaf模板的案例分析
這篇文章主要介紹了spring boot 項目中使用thymeleaf模板的案例分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java異步編程之Callbacks與Futures模型詳解
這篇文章主要為大家詳細介紹了Java異步編程中Callbacks與Futures模型的使用,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-03-03

