java 中mongodb的各种操作查询的实例详解
程序员文章站
2024-03-02 15:12:10
java 中mongodb的各种操作查询的实例详解
一. 常用查询:
1. 查询一条数据:(多用于保存时判断db中是否已有当前数据,这里 is 精确匹配,模...
java 中mongodb的各种操作查询的实例详解
一. 常用查询:
1. 查询一条数据:(多用于保存时判断db中是否已有当前数据,这里 is 精确匹配,模糊匹配 使用regex...)
public pageurl getbyurl(string url) { return findone(new query(criteria.where("url").is(url)),pageurl.class); }
2. 查询多条数据:linkurl.id 属于分级查询
public list<pageurl> getpageurlsbyurl(int begin, int end,string linkurlid) { query query = new query(); query.addcriteria(criteria.where("linkurl.id").is(linkurlid)); return find(query.limit(end - begin).skip(begin), pageurl.class); }
3.模糊查询:-----关键字---regex
public long getprocesslandlogscount(list<condition> conditions) { query query = new query(); if (conditions != null && conditions.size() > 0) { for (condition condition : conditions) { query.addcriteria(criteria.where(condition.getkey()).regex(".*?\\" +condition.getvalue().tostring()+ ".*")); } } return count(query, processlandlog.class); }
最下面,我在代码亲自实践过的模糊查询,只支持字段属性是字符串的查询,你要是查字段属性是int的模糊查询,还真没辙。
4.gte: 大于等于,lte小于等于...注意查询的时候各个字段的类型要和mongodb中数据类型一致
public list<processlandlog> getprocesslandlogs(int begin,int end,list<condition> conditions,string orderfield,direction direction) { query query = new query(); if (conditions != null && conditions.size() > 0) { for (condition condition : conditions) { if(condition.getkey().equals("time")){ query.addcriteria(criteria.where("time").gte(condition.getvalue())); //gte: 大于等于 }else if(condition.getkey().equals("inserttime")){ query.addcriteria(criteria.where("inserttime").gte(condition.getvalue())); }else{ query.addcriteria(criteria.where(condition.getkey()).is(condition.getvalue())); } } } return find(query.limit(end - begin).skip(begin).with(new sort(new sort.order(direction, orderfield))), processlandlog.class); } public list<dpsland> getdpslandsbytime(int begin, int end, date begindate,date enddate) { return find(new query(criteria.where("updatetime").gte(begindate).lte(enddate)).limit(end - begin).skip(begin), dpsland.class); }
查询字段不存在的数据 -----关键字---not
public list<goodsdetail> getgoodsdetails2(int begin, int end) { query query = new query(); query.addcriteria(criteria.where("goodssummary").not()); return find(query.limit(end - begin).skip(begin),goodsdetail.class); }
查询字段不为空的数据 -----关键字---ne
criteria.where("key1").ne("").ne(null)
查询或语句:a || b ----- 关键字---oroperator
criteria criteria = new criteria(); criteria.oroperator(criteria.where("key1").is("0"),criteria.where("key1").is(null));
查询且语句:a && b ----- 关键字---and
criteria criteria = new criteria(); criteria.and("key1").is(false); criteria.and("key2").is(type); query query = new query(criteria); long totalcount = this.mongotemplate.count(query, xxx.class);
查询一个属性的子属性,例如:查下面数据的key2.keya的语句
var s = { key1: value1, key2: { keya: valuea, keyb: valueb } }; @query("{'key2.keya':?0}") list<asset> findallbykeya(string keya);
5. 查询数量:----- 关键字---count
public long getpageinfoscount(list<condition> conditions) { query query = new query(); if (conditions != null && conditions.size() > 0) { for (condition condition : conditions) { query.addcriteria(criteria.where(condition.getkey()).is(condition.getvalue())); } } return count(query, pageinfo.class); }
查找包含在某个集合范围:----- 关键字---in
criteria criteria = new criteria(); object [] o = new object[]{0, 1, 2}; //包含所有 criteria.and("type").in(o); query query = new query(criteria); query.with(new sort(new sort.order(direction.asc, "type"))).with(new sort(new sort.order(direction.asc, "title"))); list<widgetmonitor> list = this.mongotemplate.find(query, widgetmonitor.class);
6. 更新一条数据的一个字段:
public writeresult updatetime(pageurl pageurl) { string id = pageurl.getid(); return updatefirst(new query(criteria.where("id").is(id)),update.update("updatetime", pageurl.getupdatetime()), pageurl.class); }
7. 更新一条数据的多个字段:
//调用更新 private void updateprocesslandlog(processlandlog processlandlog, int crawlresult) { list<string> fields = new arraylist<string>(); list<object> values = new arraylist<object>(); fields.add("state"); fields.add("result"); fields.add("time"); values.add("1"); values.add(crawlresult); values.add(calendar.getinstance().gettime()); processlandlogreposity.updateprocesslandlog(processlandlog, fields, values); } //更新 public void updateprocesslandlog(processlandlog land, list<string> fields,list<object> values) { update update = new update(); int size = fields.size(); for(int i = 0 ; i < size; i++){ string field = fields.get(i); object value = values.get(i); update.set(field, value); } updatefirst(new query(criteria.where("id").is(land.getid())), update,processlandlog.class); }
8. 删除数据:
public void deleteobject(class<t> clazz,string id) { remove(new query(criteria.where("id").is(id)),clazz); }
9.保存数据:
//插入一条数据 public void saveobject(object obj) { insert(obj); } //插入多条数据 public void saveobjects(list<t> objects) { for(t t:objects){ insert(t); } }
我自己使用的例子:
下面例子涉及到:
精确查询:is;
模糊查询:regex;
分页查询,每页多少:skip,limit
按某个字段排序(或升或降):new sort(new sort.order(sort.direction.asc, "port"))
查询数量:count
public map<string, object> getappportdetailbypage(int pageno, int pagesize, string order, string sortby, string appporttype, string appportseachername) { criteria criteria = new criteria(); if (!appporttype.equals("")) { if (!appporttype.equals("all")) { //db表里的字段----appmanagetype //下同 port protocol 也是db表的字段 criteria.and("appmanagetype").is(appporttype); } } if (!appportseachername.equals("")) { try { criteria.oroperator(criteria.where("port").is(integer.parseint(appportseachername)), criteria.where("protocol").regex(".*?" + appportseachername + ".*")); }catch (exception e){ criteria.oroperator(criteria.where("protocol").regex(".*?" + appportseachername + ".*")); } } map<string, object> result = maps.newhashmap(); query query = new query(criteria); query.skip((pageno - 1) * pagesize); query.limit(pagesize); if(order != null && sortby != null){ query.with(new sort(new sort.order(order.equals("asc") ? sort.direction.asc : sort.direction.desc, sortby))); }else { query.with(new sort(new sort.order(sort.direction.asc, "port"))); } list<appportmanage> list = this.mongotemplate.find(query, appportmanage.class); long count = this.mongotemplate.count(query, appportmanage.class); result.put("datas", list); result.put("size", count); return result; }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: MySQL数学函数简明总结