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…

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
首页
微信
电话
搜索