Java操作MongoDB隱約查詢和分頁查詢。本站提示廣大學習愛好者:(Java操作MongoDB隱約查詢和分頁查詢)文章只能為提供參考,不一定能成為您想要的結果。以下是Java操作MongoDB隱約查詢和分頁查詢正文
本文實例為年夜家分享了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隱約查詢和分頁查詢的完成代碼,願望對年夜家的進修有所贊助。