本文執行個體為大家分享了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(),返回多少條記錄
代碼執行個體:
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(); // 與目標資料庫能夠建立的最大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(); 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); } } /** * 記錄總數查詢 * * @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模糊查詢和分頁查詢的實現代碼,希望對大家的學習有所協助。