C # developing Mongo notes article 9
It is good to skip a few documents using skip. However, if there are a large number of skips, it will become very slow, because you must first find the data to be skipped and then discard the data. Most databases store more metadata in indexes for processing skip addresses. However, mongoDB does not currently support this feature, so avoid skipping too much data. The last query result can be used to calculate the next query condition. The simplest paging method is to use limit to return the first page of the result, and then return each subsequent page as the relative start offset. For example, var page1 = db. log. find (query ). skip (100 ). limit (100) However, in general, you can find a way to implement paging without using skip, which depends on the query itself. For example. The document list is displayed in descending chronological order. You can obtain the first page var page1 = db in the following way. log. find (). sort ({"createtime":-1 }). limit (100) can then use the createTime value in the last document as the most query condition to obtain the next page var lasttime = null; while (page1.hasnext () {lasttime = page1.next ();} var page2 = db. log. find ({"createtime": {"$ gt": createtime. createtime}); page2.sort ({"createtime":-1 }). limit (100); so in c #, I think it should be like this. Of course, this is only a demonstration and it is definitely not possible to query the last result in this function, otherwise, the query will be performed again. Just pass the result of the previous query. 'Copy the code public static List <Log> GetL. Ist (IMongoQuery query, int pagesize, int pagenum) {your database db = Your helperfactory. getDataBase (); collections collection = db. getCollection <Log> ("Log"); if (pagenum = 0) {return collection. findAs <Log> (query ). orderByDescending (l => l. createTime ). take (pagesize ). toList ();} else {DateTime lastTime = collection. findAs <Log> (query ). orderByDescending (l => l. createTime ). take (pagesize * pagenum ). Min (l => l. createTime); return collection. findAs <Log> (query ). where (l => l. createTime <lastTime ). orderByDescending (l => l. createTime ). take (pagesize ). toList () ;}} the method for copying the code is to copy the code int SkipNum = Convert. toInt32 (Request. form ["page"])-1; int TakeNum = Convert. toInt32 (Request. form ["rows"]); string searchstr = ""; // Request. queryString ["condition"]; IMongoQuery query = null; if (Request. queryString ["Condition"]! = Null) {searchstr = Request. queryString ["condition"]; query = Query <Log>. matches (c => c. description, new BsonRegularExpression (new Regex (searchstr);} // List <Log> list = DAL. DALLog. getList (query ). orderByDescending (l => l. createTime ). toList (); // return Content ("{\" total \ ": \" "+ list. count () + "\", \ "rows \": "+ users. take (TakeNum ). skip (TakeNum * SkipNum ). toJson (). toString () + "}"); StringBuilder sb = new StringBuilder (); sb. append ("{\" total \ ": \" "+ DAL. DALLog. getCount (query) + "\", \ "rows \": ["); List <Log> list = DAL. DALLog. getList (query, TakeNum, SkipNum );