mongodb分页查询优化

发布时间: 2023-11-21 13:14 阅读: 文章来源:1MUMB4984PS

1.准备环境

1.1 mongodb下载

1.2 mongodb启动

C:\mongodb\bin\mongod --dbpath D:\mongodb\data

1.3 可视化mongo工具Robo 3T下载

2.准备数据

org.mongodb mongo-java-driver 3.6.1

java代码执行

public static void main(String[] args) { try { /**** Connect to MongoDB ****/ // Since 2.10.0, uses MongoClient MongoClient mongo = new MongoClient("localhost", 27017); /**** Get database ****/ // if database doesn‘t exists, MongoDB will create it for you DB db = mongo.getDB("www"); /**** Get collection / table from ‘testdb‘ ****/ // if collection doesn‘t exists, MongoDB will create it for you DBCollection table = db.getCollection("person"); /**** insert ****/ // create a document to store key and value BasicDBObject document=null; for(int i=0;i {:$gt => last_id_object}}).limit(batch_size) end collection_name = collection[:name] @logger.debug("collection_data is: #{@collection_data}") last_id = @collection_data[index][:last_id] #@logger.debug("last_id is #{last_id}", :index => index, :collection => collection_name) # get batch of events starting at the last_place if it is set last_id_object = last_id if since_type == ‘id‘ last_id_object = BSON::ObjectId(last_id) elsif since_type == ‘time‘ if last_id != ‘‘ last_id_object = Time.at(last_id) end end cursor = get_cursor_for_collection(@mongodb, collection_name, last_id_object, batch_size)

使用java实现

import java.net.UnknownHostException;import java.util.List;import org.bson.types.ObjectId;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.MongoClient;import com.mongodb.MongoException;public class Test { public static void main(String[] args) { int pageSize=50000; try { /**** Connect to MongoDB ****/ // Since 2.10.0, uses MongoClient MongoClient mongo = new MongoClient("localhost", 27017); /**** Get database ****/ // if database doesn‘t exists, MongoDB will create it for you DB db = mongo.getDB("www"); /**** Get collection / table from ‘testdb‘ ****/ // if collection doesn‘t exists, MongoDB will create it for you DBCollection table = db.getCollection("person"); DBCursor dbObjects; Long cnt=table.count(); //System.out.println(table.getStats()); Long page=getPageSize(cnt,pageSize); ObjectId lastIdObject=new ObjectId("5bda8f66ef2ed979bab041aa"); for(Long i=0L;i
•••展开全文
相关文章