欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java操作MongoDB模糊查询和分页查询

程序员文章站 2024-03-11 18:51:19
本文实例为大家分享了java操作mongodb模糊查询和分页查询,供大家参考,具体内容如下 模糊查询条件: 1、完全匹配 pattern pattern = patt...

本文实例为大家分享了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模糊查询和分页查询的实现代码,希望对大家的学习有所帮助。