C # Development MONGO Note Nineth

Source: Internet
Author: User

Skipping a few documents with Skip is good. But if the number is very large, skip becomes very slow, because you need to find the data that needs to be skipped before discarding it. Most databases store more metadata in the index to handle skip,

But MongoDB is not currently supported, so try to avoid skipping too much data. You can usually use the last result to calculate the next query condition.

The simplest method of paging is to return the first page of the result with limit and then return each subsequent page as a relative starting offset.

such as Var page1=db. Log.find (query). Skip (+). Limit (100)

However, it is generally possible to find a page that implements paging without using skip, depending on the query itself. For example. To display a list of documents in descending order of time. You can get the first page of results in the following ways

var page1=db. Log.find (). Sort ({"Createtime": -1}). Limit (100)

You can then use the value of the createtime in the last document to query the criteria to get 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 probably be this way, of course, this is only a demonstration, it must not be in this function to query the results of the last time, or again query, directly to the last query results pass over the good '

    Public StaticList<log> GetList (imongoquery query,intPageSizeintpagenum) {Mongodatabase db=mongohelperfactory.getdatabase (); Mongocollection Collection= db. Getcollection<log> ("Log"); if(pagenum==0)            {                returnCollection. 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); returnCollection. Findas<log> (query). Where (L = l.createtime < Lasttime). OrderByDescending (L =l.createtime). Take (pagesize).            ToList (); }                }

This is called the method.

 intSkipnum = Convert.ToInt32 (request.form["page"]) -1; intTakenum = Convert.ToInt32 (request.form["rows"]); stringSearchstr ="";//request.querystring["condition"];imongoquery Query=NULL; if(request.querystring["condition"] !=NULL) {Searchstr= request.querystring["condition"]; Query= Query<log>. Matches (c = c.description,NewBsonregularexpression (NewRegex (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 =NewStringBuilder (); Sb. Append ("{\ "total\": \ ""+ DAL. Dallog.getcount (query) +"\ ", \" rows\ ": ["); List<Log> list = DAL. Dallog.getlist (query, Takenum, skipnum);

C # Development MONGO Note Nineth

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.