MongodbTemplate的查詢操作

2021-09-21 04:03:41 字數 4055 閱讀 3130

class類

@document(collection = "class")

public

class

class

public

void

setclassid(string classid)

public string getteacher()

public

void

setteacher(string teacher)

public listgetstudents()

public

void

setstudents(liststudents)

@override

public string tostring()

}

查詢滿足條件的資料

query query = query.query(criteria.where("classid").is("1"));

listclasss = mongotemplate.find(query,class.class,"class");

system.out

.println(classs);

查詢滿足條件的第一條資料

query query = query.query(criteria.where("classid").is("1"));

listclasss = mongotemplate.findone(query,class.class,"class");

system.out

.println(classs);

篩選要查詢的字段(id欄位不查出來)

dbobject dbobject = new basicdbobject();

dbobject.put("classid", "1");

dbobject fieldobject = new basicdbobject();

fieldobject.put("_id", false);

query query = new basicquery(dbobject, fieldobject);

listclasss = mongotemplate.find(query,hashmap.class,"class");

system.out.println(classs);

當然也可以不使用hashmap,而使用自己封裝的dto,那麼只會查詢出dto中的字段。

查詢結果排序

dbobject dbobject = new basicdbobject();

dbobject.put("classid", "1");

dbobject fieldobject = new basicdbobject();

fieldobject.put("_id", false);

query query = new basicquery(dbobject);

query.with(new sort(direction.asc,"classid"));

listclasss = mongotemplate.find(query,hashmap.class,"class");

system.out.println(classs);

多欄位排序

dbobject dbobject = new basicdbobject();

dbobject.put("classid", "1");

dbobject fieldobject = new basicdbobject();

fieldobject.put("_id", false);

query query = new basicquery(dbobject);

list.order> orders = new arraylist<>();

orders.add(new sort.order(direction.desc, "classid"));

orders.add(new sort.order(direction.desc, "teacher"));

query.with(new sort(orders));

listclasss = mongotemplate.find(query,hashmap.class,"class");

system.out

.println(classs);

查詢結果分頁

int pagenum = 1

;int pagesize = 10

;dbobject dbobject = new basicdbobject();

dbobject.put("classid", "1");

dbobject fieldobject = new basicdbobject();

fieldobject.put("_id", false);

query query = new basicquery(dbobject,fieldobject);

list.order> orders = new arraylist<>();

orders.add(new sort.order(direction.asc, "classid"));

orders.add(new sort.order(direction.desc, "teacher"));

query.with(new sort(orders));

query.skip(pagesize*(pagenum-1)).limit(pagesize);

listclasss = mongotemplate.find(query,hashmap.class,"class");

system.out

.println(classs);

還有一種通過morphia框架的做法:

int pagenum = 1

;int pagesize = 10

;dbobject dbobject = new basicdbobject();

dbobject.put("teacher", "mr.wang");

dbobject fieldobject = new basicdbobject();

fieldobject.put("_id", false);

fieldobject.put("_class", false);

dbobject sortdbobject=new basicdbobject();

sortdbobject.put("classid",1);

dbcursor result = mongotemplate.getcollection("class")

.find(dbobject, fieldobject)

.sort(sortdbobject)

.skip(pagesize*(pagenum-1)).limit(pagesize);

system.out

.println(result.count());

system.out

.println(result.toarray());

多條件查詢

我們只需要對query物件做出處理即可

criteria criteria = new criteria(); criteria.oroperator(criteria.where("classid").is("2"),criteria.where("teacher").is("mr.wang"));

criteria.andoperator(criteria.where("classid").is("2"));

query.addcriteria(criteria);

慢查詢實操

面試的時候問的多了,實操經驗太少了,沒有底氣,這裡具體的做一次 在my.ini中新增幾行 定義查詢多少秒的查詢算是慢查詢 long query time 2 配置慢查詢記錄檔案 slow query log on slow query log file msql slow query log 記錄下...

jQuery中的DOM操做 樣式操做

利用attr 方法來實現該功能 1 獲取 var p class p attr class 獲取p元素的class 2 設定 var p class p attr class high p元素的class為high利用addclass 方法來實現該功能 p addclass another 為p元素...

pyspark dataframe的常用操作

1 列重新命名 train data train data.todf imei pkgname timestamp 2 刪除某一列 df df.drop col 3 選取某些列 train data train data.filter train data.date 2021 03 01 4 自定義...