Java Operation MongoDB Fuzzy query and paging query _java

Source: Internet
Author: User
Tags mongoclient mongodb throw exception

This example for you to share the Java Operation MongoDB Fuzzy query and paging query for your reference, the specific contents are as follows

Fuzzy query conditions:
1. Perfect Match
Pattern pattern = pattern.compile ("^name$", pattern.case_insensitive);
2, right matching
Pattern pattern = pattern.compile ("^.*name$", pattern.case_insensitive);
3, left match
Pattern pattern = pattern.compile ("^name.*$", pattern.case_insensitive);
4, Fuzzy matching
Pattern pattern = pattern.compile ("^.*name8.*$", pattern.case_insensitive);
Total Records query:
Count (), returns the total number of queries.
Query Record sorting:
Basicdbobject sort = new Basicdbobject ();
Sort.put ("name", 1);
1, positive sequence;-1. Indicates reverse order
Paging Query:
Skip (), how many records are skipped
Limit (), how many records are returned

Code instance:


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<Mongo
      Credential> ();
      String username = "root"; String Database = "DemO ";
      char[] Password = "root123". ToCharArray ();
      Mclist.add (mongocredential.createcredential (username, database,password)); ===================================================//Mongoclientoptions.builder Builder =
      Mongoclientoptions.builder (); 
      The maximum number of connection that can be established with the target database is builder.connectionsperhost (50); 
      If all current connection are in use, 50 threads can be queued for builder.threadsallowedtoblockforconnectionmultiplier (50) on each connection; When a thread accesses a database, the maximum wait time before a successful access to an available database connection is 2 minutes or more dangerous, if more than Maxwaittime did not get this connection, the thread will throw exception/ 
      The Maxwaittime set here should be large enough to prevent database access failures due to excessive queuing of threads builder.maxwaittime (1000*60*2);  
      The timeout with the database connection is set to 1 minutes 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 Getmongo
  Client (). GETDB (dbname); }/** * @param db */public static void collections (DB db) {set<string> colls = Db.getcollectionna
    Mes ();
    for (String collname:colls) {System.out.println (collname);
    /** * Records Totals query * * @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 ("total:" + Count + "a"); /** * Fuzzy Query * * @param DB * @param name */public static void query (DB db,string name) {D
    Bcollection dbcoll = db.getcollection (name);
    Perfectly matched//pattern pattern = pattern.compile ("^name$", pattern.case_insensitive);
    Right matching//pattern pattern = pattern.compile ("^.*name$", pattern.case_insensitive); Left match//pattern pattern = Pattern.compiLe ("^name.*$", pattern.case_insensitive);
    Fuzzy matching pattern pattern = pattern.compile ("^.*name8.*$", pattern.case_insensitive);
    Basicdbobject query = new Basicdbobject ();
    Query.put ("name", pattern);
    Basicdbobject sort = new Basicdbobject ();
    1, indicating positive sequence, 1, which indicates reverse 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 ("total:" + Count + "a");  /** * Paging Query * * @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 ("total:" + Count + "a"); /** * @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);
 }
 
}

Above is the Java Operation MongoDB Fuzzy query and paging query implementation code, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.