1024programmer Mongodb The general method of using Java to operate MongoDB query

The general method of using Java to operate MongoDB query

Java operates mongodb to query, and the common filter conditions are set as follows:

List of conditions:
BasicDBList cOndList= new BasicDBList();
Temporary condition object:
BasicDBObject cOnd= null;
DBCollection coll = db. getCollection(“A”);

1. $where
In a certain application, if you want to set A to query documents and satisfy certain attribute operation results in the documents, you can write a script function and use where to set it, for example:
User information stored in a collection, including name, age, mobile phone number, address, etc., to filter out users whose age is greater than 20 and less than or equal to 40, we can do this:
String ageStr = “function (){return parseFloat(this.age) > 20
&& parseFloat(this.age) <= 40};";
cOnd= new BasicDBObject();
cond. put(“$where”, ageStr);

Put in conditional list
condList. add(cond);

2. $in
In example 1, to query user information with ages 23, 40, and 50, we can do this:
Create a temporary condition list object and add condition values ​​to it
BasicDBList values ​​= new BasicDBList();
values.add(23);
values.add(40);
values. add(50);

cOnd= new BasicDBObject();
cond.put(“age”,new BasicDBObject(“$in”,values));

Put in conditional list
condList. add(cond);

3. Fuzzy matching
In example 1, to perform fuzzy query according to the user’s name, such as: Wang, we can do this:

Use case-insensitive fuzzy queries
3.1 Exact match
Pattern pattern = Pattern.compile(“^王$”,
Pattern.CASE_INSENSITIVE);
3.2 Right match
Pattern pattern = Pattern.compile(“^.*王$”,
Pattern.CASE_INSENSITIVE);
3.3 Left match
Pattern pattern = Pattern.compile(“^王.*$”,
Pattern.CASE_INSENSITIVE);
3.4 Fuzzy matching
Pattern pattern = Pattern.compile(“^.*王.*$”,
Pattern.CASE_INSENSITIVE);

cOnd= new BasicDBObject();
cond. put(“name”,cond);

Put in conditional list
condList. add(cond);

4. $gte/$lte/$gt/$lt
In example 1, to filter out users whose age is greater than 20 and less than or equal to 40, we can do this:
cOnd= new BasicDBObject();
cond.append(“age”,new BasicDBObject(“$gt”,20));
cond.append(“age”,new BasicDBObject(“$lte”,40));

Put in conditional list
condList. add(cond);

There may be multiple conditions for filtering in daily queries, and the relationship between multiple conditions is and, combined with the above examples 1, 2, 3, and 4
Adding each condition to the list of conditions, I can do this:
BasicDBObject searchCOnd= new BasicDBObject();
searchCond. put(“$and”, condList);

Then query the data:
DBCursor ret = coll. find(searchCond);

The above is the setting method of commonly used filter conditions, and others will be updated later.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/the-general-method-of-using-java-to-operate-mongodb-query/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索