MongoDB模糊查詢java實現樣例
阿新 • • 發佈:2018-12-24
在我的mongoDB中北京共有16個區縣
其中只有密雲和延慶是縣,其他的為區,現在要將這兩個例外查出來
直接用mongoDB shell命令是這樣查詢的:
db.country.find({'name':{$regex:/縣/},'sheng':'11','level':3})
java實現如下:
package com.adtec.mongodb;
import java.util.Iterator;
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;
/**
*
* @author 浪丶蕩
* <br>
* 模糊查詢例項
*
*/
public class BlurQuery {
private String host = "192.168.71.193";
private int port = 27017;
public static void main(String[] args) {
BlurQuery blurQuery = new BlurQuery();
//資料庫test
String dbname = "test";
DB db = blurQuery.getDB(dbname);
//集合country
String collectionName = "country";
//獲取結果集
DBCursor cur = blurQuery.query(db, collectionName);
//show一下結果
blurQuery.printDBCursor(cur);
}
/**
*
* @param dbname
* @return 獲取一個與指定資料庫相關聯的DB物件
*/
public DB getDB(String dbname) {
@SuppressWarnings("resource")
MongoClient mongoClient = new MongoClient(host, port);
@SuppressWarnings("deprecation")
DB db = mongoClient.getDB(dbname);
return db;
}
/**
*
* @param db
* @param name
* @return DBCursor
* 查詢北京市縣級單位帶"縣"字的城市<br>
* mongodb vue 是db.country.find({'name':{$regex:/縣/},'sheng':'11','level':3})
*/
public DBCursor query(DB db, String collectionName) {
DBCollection dbColl = db.getCollection(collectionName);
// 模糊匹配正則規則(只要名字裡帶縣字)
Pattern pattern = Pattern.compile("^.*縣.*$", Pattern.CASE_INSENSITIVE);
BasicDBObject query = new BasicDBObject();
//加入查詢條件
query.put("name", pattern);
query.put("sheng", "11");
query.put("level", 3);
//按名次升序排序
BasicDBObject sort = new BasicDBObject();
// 1,表示正序; -1,表示倒序
sort.put("name", 1);
DBCursor cur = dbColl.find(query).sort(sort);
return cur;
}
/**
* 遍歷結果集
* @param cur
*/
private void printDBCursor(DBCursor cur) {
Iterator<DBObject> iterator = cur.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
結果:
十一月 21, 2017 3:12:16 下午 com.mongodb.diagnostics.logging.JULLogger log
資訊: Cluster created with settings {hosts=[192.168.71.193:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
十一月 21, 2017 3:12:16 下午 com.mongodb.diagnostics.logging.JULLogger log
資訊: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=192.168.71.193:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
十一月 21, 2017 3:12:16 下午 com.mongodb.diagnostics.logging.JULLogger log
資訊: Opened connection [connectionId{localValue:1, serverValue:14}] to 192.168.71.193:27017
十一月 21, 2017 3:12:16 下午 com.mongodb.diagnostics.logging.JULLogger log
資訊: Monitor thread successfully connected to server with description ServerDescription{address=192.168.71.193:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 5]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=1732855}
十一月 21, 2017 3:12:16 下午 com.mongodb.diagnostics.logging.JULLogger log
資訊: Opened connection [connectionId{localValue:2, serverValue:15}] to 192.168.71.193:27017
{ "_id" : { "$oid" : "59ec563f937ceb812c804c65"} , "code" : "110228" , "sheng" : "11" , "di" : "02" , "xian" : "28" , "name" : "密雲縣" , "level" : 3.0}
{ "_id" : { "$oid" : "59ec563f937ceb812c804c66"} , "code" : "110229" , "sheng" : "11" , "di" : "02" , "xian" : "29" , "name" : "延慶縣" , "level" : 3.0}