需求:在MongoDB中查詢一個類型(數(shù)據(jù)庫中可能百萬條數(shù)據(jù))用作下拉列表查詢。
在工具中用 db.getCollection('order').distinct("typeName") 即可實現(xiàn)。
java 代碼:
MongoCollection
//獲得mongodb數(shù)據(jù)庫連接 getCollection表名
//this.databaseName 來自配置文件直接讀取動態(tài)配置,即數(shù)據(jù)庫名稱
Iterable iterable = coll.distinct('typeName',String);
// String 查詢的值 是什么類型,由于本次查詢的為漢字,所以是String
MongoCursor it = iterable.iterator();
while(it.hasNext()) {
String typeName = it.next().toString()
System.out.pringln(typeName)
}
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計,牡丹江網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:牡丹江等地區(qū)。牡丹江做網(wǎng)站價格咨詢:028-86922220
/*****
MongoCollection : com.mongodb.client.MongoCollection
Document:org.bson.Document
implements Map, Serializable, Bson
**/