MongoDB里面怎么 查最值

怎么在MongoDB里面怎么 查最值  就好像SQL语句的select MAX(row) form ....


我现在知道了,现在拿出来和大家分享一下吧  



DBObject ref = new BasicDBObject();

ref.put("auth_id", authId);

ref.put("weibo_type", weiboType);


DBObject keys = new BasicDBObject("weibo_id", 1);


DBObject orderBy = new BasicDBObject("weibo_id", -1);// -1标示降序 1表示升序


DBCursor dbc = statusColl.find(ref, keys).sort(orderBy).limit(1);


DBObject dbo = null;

try {

dbo = dbc.next();

} catch (NoSuchElementException e) {

return -1L;

}

return (Long) dbo.get("weibo_id");


大概就是上面那个样子啦,根据您自己的业务需求,写出符合条件的代码吧,主要就是find()和limit()这两根函数而已,下面的这个大家可以无视,我是写给自己看的



List<Status> list = new LinkedList<Status>();

DBObject ref = new BasicDBObject();


ref.put("user_id", userId);


DBObject orderBy = new BasicDBObject("create_at", -1); // -1标示降序 1表示升序


DBCursor res = homePagesColl.find(ref).sort(orderBy).skip(

(pageNumber - 1) * nPerPage).limit(nPerPage);


while (res.hasNext()) {

Object obj = res.next().get("weibo_content");

Status sta = new Status(new JSONObject(obj.toString()));

list.add(sta);

}

return list;

 

你可能感兴趣的:(sql,mongodb)