Home > Java > javaTutorial > body text

Sample code sharing for operating mongoDB query in Java

黄舟
Release: 2017-09-25 10:19:12
Original
1730 people have browsed it

This article mainly introduces relevant information about detailed examples of java operation mongo query. I hope this article can help everyone. Friends in need can refer to

detailed examples of java operation mongo query.

Foreword:

MongoDB is a database based on distributed file storage. Written in C++ language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB is a product between a relational database and a non-relational database. It is the most feature-rich and most like a relational database among non-relational databases. The data structure it supports is very loose and is a bson format similar to json, so it can store more complex data types. The biggest feature of Mongo is that the query language it supports is very powerful. Its syntax is somewhat similar to an object-oriented query language. It can almost implement most functions similar to single-table queries in relational databases, and it also supports indexing of data.

It is very convenient to query in the visualization tool, similar to {' key' : ' value' }. This kind of query is also similar to advanced query. For example, if the value of a certain field is greater than 5, we will You can query {' key' : { $gt : 5} }, thanks to monggo's collection-oriented storage, which stores object type data (binary data BSON of JSON). This makes the query very convenient, and other advanced queries, For example, if it is less than $lt, greater than or equal to $gte, less than or equal to $lte, in a certain range $in, not in a certain range $nin, etc., they can all be queried through the above method.

Now show how to manipulate mongo query through java through some code in java. It includes exact matching query, advanced query, query within two time ranges, fuzzy query, paging implementation and other queries.

See the code for details.


try { 
  System.out.println("=========********测试3开始**********===========*************"); 
  //先获取mongo库的集合DBCollection对象 
  DBCollection data = (DBCollection) mongoClientService.getColectionByName("mongo库集合名称"); 
   
  BasicDBObject query3 = new BasicDBObject(); 
  query3.put("name","Tom");//查找姓名为Tom 
 
  //查询两个时间范围的,用map包装一下 
  Map<String, Object> queryMap = new HashMap<>(); 
  queryMap.put("$gt", "1496911821071"); 
  queryMap.put("$lt", "1496915447871"); 
    query3.put("timeStamp", new BasicDBObject(queryMap)); 
    //模糊匹配rule查询 
    query3.put("businessRuleName", new BasicDBObject("$regex","rule")); 
    DBCursor result3 = data.find(query3); 
   
  String resultCode = "4"; 
  if ("4".equals(resultCode)) { 
    BasicDBList cond = new BasicDBList(); 
    cond.add("0"); 
    cond.add("1"); 
    cond.add("2"); 
    cond.add("3"); 
    query3.put("resultCode", new BasicDBObject("$nin", cond));//查询resultCode不是0,1,2,3的 
  } 
    System.out.println(result3.count());//count()方法得到查询到的记录数 
 
    result3.skip(10).limit(20);//分页,skip():从第几条开始,limit():限制返回的条数 
 
  while (result3.hasNext()) { 
    DBObject dbObject = (DBObject) result3.next(); 
    System.out.println(JSON.serialize(dbObject)); 
    System.out.println("------------"); 
  } 
  System.out.println(result3.count());// 
  System.out.println("=========********测试3结束**********===========*************"); 
} catch (Exception e) { 
  e.printStackTrace(); 
  System.out.println("---------测试3异常了----------"); 
}
Copy after login

The above contains examples of queries in many ways. The four main classes are DBCollection, BasicDBObject, DBCursor, and BasicDBList, which are for reference only.

The above is the detailed content of Sample code sharing for operating mongoDB query in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template