java mongodb skip_关于MongoDB的skip及不使用skip分页,以及对应的java实现简例

一、MongoDB分页

1.skip实现跳页(比较简单)

/**

*db.getCollection('user').find({})是指查询全部。

*sort()设置排序,本示例是指以_id作为条件,正序排序。若将数字1改为-1,则为倒序。

*skip()设置跳页,本示例是指跳过前10条,从第11条开始显示。

*limit()设置每页的显示数量,本例是指每页限制显示10条。*/db.getCollection('user').find({}).sort({"ID":1}).skip(10).limit(10)

2.非skip实现跳页

原理:以自增_id作为主条件,获取前一页的最后一条记录,查询之后的指定条记录

//根据_id,查询前10条

var a = db. getCollection('user').find({}).sort("_id",1).limit(10)//定义变量last

var last = null

//循环遍历

while(a.hasNext()){

last=a.next;//循环到最后,last接收的是最后一条的信息

}//核心是"_id":{"$gt":last._id},即查询大于最后一条的_id的后10条信息

db.getCollection('slt').find({"_id":{"$gt":last._id}}).sort({_id:1}).limit(10)

二、Java实现MongoDB查询分页

说明:使用skip跳过少量的数据是很好的选择,但是如果跳过大量的数据的时候,skip方法就会执行的很慢。所以我们要尽量的避免使用skip跳过大量的数据。

原理:MongoDB查询语句的java实现

1.skip实现跳页 (部分代码)

a.连接数据库

privateDaoMongoClient daoMongoClient;private MongoCollection getMdbCollection() throwsException {//连接到 mongodb服务(本地连接)

MongoClient mongoClient = new MongoClient("localhost", 27017);

MongoDatabase mdb= mongoClient.getDatabase("myCollection");//创建集合

MongoCollection mongoCollection = mdb.getCollection("user");if (null ==mongoCollection) {

mdb.createCollection("myCollection");

}returnmongoCollection;

}

b.查询,获得一个数据的集合(简例,可以根据具体条件变化)

private List findOrderList(Map paramOptions,int page,int size) throwsException {//调用方法,连接数据库

MongoCollection mongoCollection =getMdbCollection();//查询

MongoCursor iterable = mongoCollection.find().skip((page - 1) * size).limit(size).sort(new BasicDBObject("_id", 1)).iterator();//定义orderItems,用以接收查询的信息

List orderItems = new ArrayList();//遍历

while(iterable.hasNext()) {

Document next=iterable.next();

JSONObject a=JSONObject.parseObject(next.toJson());

orderItems.add(a);

}returnorderItems;

}

2.非skip实现跳页(以自增_id作为主条件)

a.连接数据库

privateDaoMongoClient daoMongoClient;private MongoCollection getMdbCollection() throwsException {//连接到 mongodb服务(本地连接)

MongoClient mongoClient = new MongoClient("localhost", 27017);

MongoDatabase mdb= mongoClient.getDatabase("myCollection");//创建集合

MongoCollection mongoCollection = mdb.getCollection("user");if (null ==mongoCollection) {

mdb.createCollection("myCollection");

}returnmongoCollection;

}

b.查询,获得一个数据的集合(简例,可以根据具体条件变化)

private List findOrderList(Map paramOptions,int page,int size,String sidx,String sord) throwsException {//连接数据库

MongoCollection mongoCollection =getMdbCollection();//进行第一次查询,条件中没有skip

MongoCursor iterable = mongoCollection.find().limit(size).sort(new BasicDBObject("_id", 1)).iterator();//定义orderItems,用以接收查询的信息

List orderItems = new ArrayList();//如果只有一页或第一页,则走此条件,否,则走else

if(page == 1){//遍历

while(iterable.hasNext()) {

Document next=iterable.next();

JSONObject a=JSONObject.parseObject(next.toJson());

orderItems.add(a);

}

}else{

MongoCursor iterable2 = mongoCollection.find().limit(size*(page-1)).sort(new BasicDBObject("_id", 1)).iterator();//定义变量last,用以存储每页的最后一条记录

Document last = null;

while (iterable2.hasNext()) {

last =iterable2.next();

}//定义condition,用以添加【$gt:每页最后一条记录的_id值】,作为查询条件

Map condition = new HashMap<>();

if (null != last.get("_id")) {

condition.put("$gt",last.get("_id"));

}

MongoCursor iterable3 = mongoCollection.find(condition).limit(size).sort(new BasicDBObject("_id", 1)).iterator();

//遍历

while(iterable.hasNext()) {

Document next =iterable.next();

JSONObject a =JSONObject.parseObject(next.toJson());

orderItems.add(a);

}

return orderItems;

}

注:上面是按_id正序排列的,如果想要按照倒序排列,则需要将condition.put("$gt",last.get("_id"))改为condition.put("$lt",last.get("_id")),将sort(new BasicDBObject("_id", 1)改为sort(new BasicDBObject("_id", -1)。

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