MongoDB-bottom generic class sharing with C # encapsulation

Source: Internet
Author: User
Tags findone mongodb serialization tojson

This is mainly about the bottom of my package, does not involve MongoDB installation, start-up, visual query and other things, will be accompanied by a number of reference addresses for everyone to study together.

There are currently two main types of drivers available for MongoDB:

1. Official website driver   Download Address: http://github.com/mongodb/mongo-csharp-driver/downloads
2. Third-party Samus driver download address: Https://github.com/samus/mongodb-csharp

Two drivers have been used in their own use, personal sense of the official drive to provide a more convenient way to use it , and newer than Samus, so they are using the official drive.

Official website-driven simple use

mainly uses the following two DLLs

mongodb.dll           Driven Main program
mongodb.gridfs.dll    is used to store large files.

Basic additions and deletions to the code are as follows:

Database connection string const string strconn =  "mongodb://127.0.0.1:27017";
Database name const string dbname =  "Cnblogs";
Define Database mongodatabase db;  <summary>///  Open the database link/// </summary> public void getconnection () {//
Define Mongo service  mongo mongo = new mongo (strconn); Open the connection  mongo.
Connect (); Obtains the database cnblogs, if does not exist then automatically creates the  db = mongo.
Getdatabase (dbname)  as MongoDatabase; /// <summary>///  Add Data/// </summary> Public void insert () {Var col  = db.
Getcollection<users> (); or  //var col = db.
GetCollection ("Users");
Users users = new users (); Users.
name =  "Xumingxiang"; Users.
sex =  "Man"; Col.
Insert (users); /// <summary>///  Update Data/// </summary> public void update () {Var col  = db.
Getcollection<users> (); //detect the first record of the name value of Xumingxiang  users users = col.
FindOne (x => x.name ==  "Xumingxiang"); or  //users users = col.
FindOne (new document { {  "Name",  "Xumingxiang"  } });  users.
sex =  "Women"; Col.
Update (users, x => x.sex ==  "man"); /// <summary>///  Delete Data/// </summary> public void delete () {Var col  = db.
Getcollection<users> (); Col.
Remove (x => x.sex ==  "man"); Or////find the first record of the name value Xumingxiang  //users users = col.
FindOne (x => x.sex ==  "man"); Col.
Remove (users); /// <summary>///  Query Data/// </summary> public void query () {Var col  = db.
Getcollection<users> ();
var query = new document { {  "Name",  "Xumingxiang"  } }; InquireSpecify all data  var result1 = col for the query criteria.
Find (query); query specifies the first data  var result2 = col of the query criteria.
FindOne (query); Query the data  var result3 = col in all collections.
FindAll (); }



Encapsulation Extension Use

1. Database configuration file

Consider the possibility of using a different database in a project, such as: ordinary data and file data, etc. are stored in different databases, and may also query across servers, so here first create a profile help class, mainly can be a number of database configuration to meet the requirements across the server, across the data.

The configuration format is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<ServiceConfig>
<mongodbs>
<item dbname= " MyDb "Hostname=" mongodb://127.0.0.1:27017 "></Item> <item dbname=" myDb1 "hostname=" mongodb://
127.0.0.1:27017 "></Item>
<item dbname=" myDb2 "hostname=" mongodb://127.0.0.1:27017 "></item" >
</mongodbs>
</ServiceConfig>


XML Serialization Object class

public class ServiceConfig
{
[XmlArray, XmlArrayItem (' Item ')] public
list<mongodbconfig> MongoDB s {get; set;}
}
[XmlRoot]
public class Mongodbconfig
{
[XmlAttribute (' dbname ')] public
string dbname {get; set;}
[XmlAttribute ("HostName")]
public string HostName {get; set;}
}


Read configuration file Management class

Public&nbsp;class&nbsp;managerconfig {&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;string&nbsp;ConfigPath;
&nbsp;&nbsp;&nbsp;&nbsp;//Load configuration file &nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;managerconfig () &nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ConfigPath&nbsp;=&nbsp; "./config.xml"; &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;//xml object after serialization &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;
serviceconfig&nbsp;_settings;
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;serviceconfig&nbsp;servicesettings &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;_settings&nbsp;??
&nbsp; (_settings&nbsp;=&nbsp;load ()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;// Load XML serialization to ServiceConfig Object &nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;serviceconfig&nbsp;load () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (file.exists (configpath)) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using&nbsp; (Filestream&nbsp;fs&nbsp;=&nbsp;new&nbsp;filestream (configpath,&nbsp;filemode.open)) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlserializer&nbsp;xs&nbsp;=&nbsp;new&nbsp;xmlserializer (
typeof (ServiceConfig));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//serialized as an object &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_settings &nbsp;=&nbsp; (ServiceConfig) xs.
Deserialize (FS); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eLSE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;exception ("Database configuration file does not exist, please check"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//_settings&nbsp;=&nbsp;new&nbsp;
ServiceConfig (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;_
Settings &NBSP;&NBSP;&NBSP;&NBSP}}


2. Entity General interface

There is no mongodb in itself, with a objectid, in order to unify each entity object has this ID, here to establish a common interface and a low-level entity base class to standardize processing

Entity interface

Public interface imongoentity
{
string Id {get;}
}


Underlying entity base class

public class basemodel:imongoentity
{
[Bsonignore] public
string Id
{
get            {
if (_id = = objectid.empty)
_id = Objectid.generatenewid (DateTime.Now);
return _id.        ToString ();
}
[Bsonid]
private ObjectId _id;
}


Examples of entity classes (inherited from the Basemodel class)

public class Userentity:basemodel
{public
string UserName {get; set;}
public int Num {get; set;}
The time stored in MongoDB is the standard time UTC +0:00 (8 hours apart)
[bsondatetimeoptions (Kind = datetimekind.local)] public
DateTime posttime {get; set;}
}


3.Mongodb Common help base class (main Class)

public&nbsp;class&nbsp;mongodbbase&lt;t&gt;&nbsp;where&nbsp;t&nbsp;:&nbsp;class,imongoentity {&nbsp;&nbsp;
&nbsp;&nbsp;protected&nbsp;MongoServer&nbsp;server&nbsp;=&nbsp;null;
&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;MongoDatabase&nbsp;db&nbsp;=&nbsp;null;
&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;MongoCollection&lt;T&gt;&nbsp;collection; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;void&nbsp;init (string&nbsp;dbname) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;item&nbsp;=&nbsp;managerconfig.servicesettings.mongodbs.where (P &nbsp;=&gt;&nbsp;p.dbname&nbsp;==&nbsp;dbname).
FirstOrDefault (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (item&nbsp;==&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new
&nbsp;exception ("There is no configuration object that has a database of:&nbsp;" &nbsp;+&nbsp;DbName&nbsp;+&nbsp; "&nbsp;, please check"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsP;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;=&nbsp;mongodb.driver.mongoserver.create (Item.hostName); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db&nbsp;=&nbsp;server.
Getdatabase (Item.dbname); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collection&nbsp;=&nbsp;db. Getcollection&lt;t&gt; (typeof (T).
Name.replace ("Entity",&nbsp; ")"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp} &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Query &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; get object by ID &nbsp;&nbsp; &nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "id" &gt;&lt;/param &gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;t &nbsp;getmodelbyid (string&nbsp;id) &nbsp;&nbsp;&nbsp;&nbsp;{&NBSp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection.
Findonebyid (ID); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Get a record (custom condition) &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt; &lt;/returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;t&nbsp;firstordefault (Expression&lt;func&lt;t,&nbsp;bool &gt;&gt;&nbsp;expression) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Mongodb.driver.imongoquery&nbsp;query&nbsp;=&nbsp;query&lt;t&gt;.
Where (expression); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection.
FindOne (query); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Get a record &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/ Returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;t&nbsp;firstordefault () &nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection. FindAll ().
FirstOrDefault ();
&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; get all &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns &gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;list&lt;t&gt;&nbsp;findall () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection. FindAll ().
ToList (); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; get all (custom condition) &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/ Returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;list&lt;t&gt;&nbsp;findall (Expression&lt;func&lt;t,&nbsp;bool &gt;&gt;&nbsp;expression) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Mongodb.driver.imongoquery&nbsp;query&nbsp;=&nbsp;query&lt; T&gt;.
Where (expression); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection. Find (query).
ToList (); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Acquire quantity &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= according to the condition "Expression" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns&gt; &nbsp;&nbsp; &nbsp;&nbsp;public&nbsp;long&nbsp;getcount (expression&lt;func&lt;t,&nbsp;bool&gt;&gt;&nbsp;expression&nbsp;= &nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (expression&nbsp;= =&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection.
Count (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&Nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection. Count (QUERY&LT;T&GT;.
Where (expression)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;
&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; based on ID to determine whether there is &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "id" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; &lt;returns&gt;&lt;/returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;bool&nbsp;exists (string&nbsp;id) &nbsp; &nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;collection.
Findonebyid (ID) &nbsp;!=&nbsp;null;
&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Paging &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= " PageIndex "&gt; Total page number &lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name=" PageSize "&gt; Page capacityQuantity &lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "rowcounts" &gt; Total record number &lt;/param&gt; &nbsp; &nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "expression" &gt; Conditions &lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;/// &nbsp;&lt;param&nbsp;name= "ISASC" &gt; is positive sequence &lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= " orderfiled &gt; Sorted fields &lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns&gt; &nbsp;&nbsp; &nbsp;&nbsp;public&nbsp;list&lt;t&gt;&nbsp;page (int&nbsp;pageindex,&nbsp;int&nbsp;pagesize,&nbsp;out&nbsp; &nbsp;long&nbsp;rowcounts,&nbsp;expression&lt;func&lt;t,&nbsp;bool&gt;&gt;&nbsp;expression&nbsp;=&nbsp;null,
&nbsp;bool&nbsp;isasc&nbsp;=&nbsp;true,&nbsp;params&nbsp;string[]&nbsp;orderfiled) &nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MongoCursor&lt;T&gt;&nbsp;mongoCursor; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Condition Selection &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (expression&nbsp;!=&nbsp;null) &NBSP;&NBsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;rowcounts&nbsp;=&nbsp;collection. Find (QUERY&LT;T&GT;. Where (expression)).
Count (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongocursor&nbsp;=&nbsp;collection. Find (QUERY&LT;T&GT;.
Where (expression)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;rowcounts&nbsp;=&nbsp;collection. FindAll ().
Count (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongocursor&nbsp;=&nbsp;collection.
FindAll (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Sort &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (orderfiled&nbsp;!=&nbsp;null&nbsp;&amp;&amp;&nbsp; orderfiled.length&nbsp;&gt;&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Handling primary key fields &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp; (int&nbsp;i&nbsp;= &nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;orderfiled.length;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;if&nbsp; (orderfiled[i). Equals ("id", &nbsp;stringcomparison.currentcultureignorecase)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OrderFiled[i]&nbsp;=&nbsp; "_id"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& Nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (ISASC) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongocursor
&nbsp;=&nbsp;mongocursor.setsortorder (sortby.ascending (orderfiled)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Mongocursor&nbsp;=&nbsp;mongocursor.setsortorder (sortby.descending (orderfiled)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;mongocursor.setskip (PageIndex&nbsp;- &nbsp;1) &nbsp;*&nbsp;pagesize). Setlimit (PageSize).
ToList (); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Low efficiency, no &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp; &nbsp;/////&nbsp; pagination &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt; Returns&gt;&lt;/returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;//public&nbsp;list&lt;t&gt;&nbsp;page (Int&nbsp;PageIndex, &nbsp;int&nbsp;PageSize,&nbsp;out&nbsp;&nbsp;long&nbsp;RowCounts,&nbsp;Expression&lt;Func&lt;T,&nbsp;bool&gt; &gt;&nbsp;expression&nbsp;=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;//{&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;
&nbsp;List&lt;T&gt;&nbsp;ret&nbsp;=&nbsp;new&nbsp;List&lt;T&gt; ();
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;IQueryable&lt;T&gt;&nbsp;queryable; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;//Condition Selection &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;if &nbsp; (expression&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queryable&nbsp;=&nbsp;cOllection. Find (QUERY&LT;T&GT;. Where (expression)).
AsQueryable ();
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;queryable&nbsp;=&nbsp;collection. FindAll ().
AsQueryable (); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;rowcounts &nbsp;=&nbsp;queryable.
Count (); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;=&nbsp;queryable. Skip ((pageindex&nbsp;-&nbsp;1) &nbsp;*&nbsp;pagesize). Take (PageSize).
ToList ();
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;ret; &nbsp;&nbsp;&nbsp;&nbsp;//} &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///// &nbsp; Pagination &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;typeparam &nbsp;name= "TKey" &gt;&lt;/typeparam&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;param&nbsp;name= "PageIndex" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;param&nbsp; Name= "PageSize" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;param&nbsp;name= "rowcounts" &gt;&lt;/ param&gt; &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;param&nbsp;name= "expression" &gt;&lt;/param&gt; &nbsp;&nbsp; &nbsp;&nbsp;/////&nbsp;&lt;param&nbsp;name= "by" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;
Param&nbsp;name= "Isorder" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;/////&nbsp;&lt;returns&gt;&lt;/returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;//public&nbsp;List&lt;T&gt;&nbsp;Page&lt;TKey&gt; (int&nbsp;pageindex,&nbsp;int&nbsp; pagesize,&nbsp;out&nbsp;&nbsp;long&nbsp;rowcounts,&nbsp;expression&lt;func&lt;t,&nbsp;bool&gt;&gt;&nbsp; Expression&nbsp;=&nbsp;null,&nbsp;expression&lt;func&lt;t,&nbsp;tkey&gt;&gt;&nbsp;orderby&nbsp;=&nbsp;null, &nbsp;bool&nbsp;isorder&nbsp;=&nbsp;true) &nbsp;&nbsp;&nbsp;&nbsp;//{&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp; &nbsp;&nbsp;list&lt;t&gt;&nbsp;ret&nbsp;=&nbsp;new&nbsp;list&lt;t&gt; ();
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;IQueryable&lt;T&gt;&nbsp;queryable; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;//Condition Selection &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;if &nbsp; (expression&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queryable&nbsp;=&nbsp;collection. Find (QUERY&LT;T&GT;. Where (expression)).
AsQueryable ();
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;queryable&nbsp;=&nbsp;collection. FindAll ().
AsQueryable ();
&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;//Sort &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (orderby&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if &nbsp; (isorder) &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queryable&nbsp;=&nbsp; Queryable.
by (by); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;queryable&nbsp;=&nbsp;queryable.
OrderByDescending (by); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp; &NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;rowcounts&nbsp;=&nbsp;queryable.
Count (); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nBsp;ret&nbsp;=&nbsp;queryable. Skip ((pageindex&nbsp;-&nbsp;1) &nbsp;*&nbsp;pagesize). Take (PageSize).
ToList ();
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;ret; &nbsp;&nbsp;&nbsp;&nbsp;//}&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp; &nbsp;&nbsp;&nbsp; #region &nbsp; Delete &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;/// &nbsp; Delete with conditions &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp; Name= "expression" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns&gt; &nbsp;
&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;delete (expression&lt;func&lt;t,&nbsp;bool&gt;&gt;&nbsp;expression) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongodb.driver.imongoquery&nbsp;query &nbsp;=&nbsp;query&lt;t&gt;.
Where (expression); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;collection.
Remove (query); &nbsp;&nbsp;&nbsp;&nBSP;} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Delete by model &nbsp;&nbsp;&nbsp;
&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "Model" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;delete (T&nbsp;model) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongodb.driver.imongoquery&nbsp;query&nbsp;=&nbsp;query&lt;t&gt;. Where (P&nbsp;=&gt;&nbsp;p.id&nbsp;==&nbsp;model.
ID); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collection.
Remove (query); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Delete by id &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= " Id "&gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;delete (string&nbsp;id) &nbsp;&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongodb.driver.imongoquery&nbsp;query&nbsp;=&nbsp;query &lt;T&gt;.
Where (P&nbsp;=&gt;&nbsp;p.id&nbsp;==&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collection.
Remove (query);
&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Delete all &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns &gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;deleteall () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;collection.
RemoveAll (); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Add &nbsp;&nbsp; &nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Single model add &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; &lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "model" &gt;&lt;/param&gt; &nbsp;&nbsp; &nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;insert (T &nbsp;model) &nbsp;&nbsp; &nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;collection.
Insert&lt;t&gt; (model);
&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Bulk add &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "Model" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;returns&gt;&lt;/returns&gt; &nbsp;&nbsp;&nbsp;&nbsp; Public&nbsp;void&nbsp;insertbatch (List&lt;t&gt;&nbsp;model) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;collection.
Insertbatch&lt;t&gt; (model); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Modify &nbsp;&nbsp; &nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Modify &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/ summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "model" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp; &nbsp;///&nbsp;&lt;returns&gt;&lt;/rEturns&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;update (T&nbsp;model) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;collection.
Save&lt;t&gt; (model);
&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Bulk modification &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;param&nbsp;name= "Model" &gt;&lt;/param&gt; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;updateall (List&lt;t&gt;&nbsp;model) &nbsp; &nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. ForEach (e&nbsp;=&gt;&nbsp;collection.
Save&lt;t&gt; (e)); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion}


4. Business Class

When you create a new table (called a collection in MongoDB), you need to manipulate it, including some business processes. Inherit the Mongodbbase class first, and then initialize the object using the Init method, such as the following Userservcices class

public class userservices:mongodbbase<userentity>
{public
userservices ()
{
this.i    NIT ("MyDb");
}
}



5. Use

[TestClass] Public&nbsp;class&nbsp;unittest1 {&nbsp;&nbsp;&nbsp;&nbsp;random&nbsp;rd&nbsp;=&nbsp;new&nbsp;random
();
&nbsp;&nbsp;&nbsp;&nbsp;userservices&nbsp;ubll&nbsp;=&nbsp;new&nbsp;userservices (); &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Add &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public &nbsp;void&nbsp; Add () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserEntity&nbsp;
Model&nbsp;=&nbsp;new&nbsp;userentity (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. username&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Num&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.
posttime&nbsp;=&nbsp;datetime.now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Insert (model); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Add complex Model () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;complexentity&nbsp;model&nbsp;=&nbsp;new&nbsp;complexentity (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complexservices&nbsp;cbll&nbsp;=&nbsp;new&nbsp;complexservices
(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.
name&nbsp;=&nbsp; "Complex"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.
Schools&nbsp;=&nbsp;new&nbsp;list&lt;school&gt; (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Schools.add (New&nbsp;school () &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;master&nbsp;=&nbsp;new&nbsp;grade () &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp; "Master" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &NBSP;&NBSP;&NBSP}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;=&nbsp; " School ",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Students&nbsp;=&nbsp;new&nbsp;List&lt;
Student&gt; () &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Schools[0].
Students.add (New&nbsp;student () &nbsp;{&nbsp;Age&nbsp;=&nbsp;22,&nbsp;Name&nbsp;=&nbsp; "John" &NBSP;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cbll.
Insert (model); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Bulk Add () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;UserEntity&gt;&nbsp;
Data&nbsp;=&nbsp;new&nbsp;list&lt;userentity&gt; (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp; (int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp; 1000000;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userentity&nbsp;model&nbsp;=&nbsp;new&nbsp;userentity (); &nbsp;&nbsp;&nbsP;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. username&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Num&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.
posttime&nbsp;=&nbsp;datetime.now;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.add (model); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Insertbatch (Data); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Modify &nbsp;&nbsp;
&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Get a single object _ Modify () &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;model&nbsp;=&nbsp;ubll.
FirstOrDefault (p&nbsp;=&gt;&nbsp;p.id&nbsp;!=&nbsp; ""); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. username&nbsp;=&nbsp; "
New1 "; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Update (model); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Batch modification () &nbsp;&nbsp;&nbsp;&nbsp;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;MODEL&NBSP;=&NBSP;UBLL .
FindAll (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp; (int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp; Model. count;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model[i].
username&nbsp;=&nbsp; "Text"; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
UPDATEALL (model); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Query &nbsp;&nbsp; &nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Get all objects () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;Model&nbsp;=&nbsp;ubll.
FindAll (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;count&nbsp;=&nbsp;model.
Count; &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Get a single object () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;model&nbsp;=&nbsp; Ubll.
FirstOrDefault (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;count&nbsp;=&nbsp;model.
Posttime; &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Get object based on ID () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;model&nbsp;=&nbsp; Ubll.
Getmodelbyid ("eeef22d6-7ac6-40cd-9312-59ab15fd904a"); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Get all pair condition like _ with condition () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;model&nbsp;= &nbsp;ubll. FindAll (P&nbsp;=&gt;&nbsp;p.usernamE.contains ("Name")); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;count&nbsp;=&nbsp;model.
Count; &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; paging ( ) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;Rows; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;UserEntity&gt;&nbsp;pageDate&nbsp;=&nbsp;new&nbsp;List&lt;UserEntity&gt;
(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pagedate&nbsp;=&nbsp;ubll.
Page (300,&nbsp;20,&nbsp;out&nbsp;rows,&nbsp;p&nbsp;=&gt;&nbsp;p.num&nbsp;&gt;&nbsp;1500); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pagedate&nbsp;=&nbsp;ubll.
Page (1,&nbsp;20,&nbsp;out&nbsp;rows,&nbsp;null,&nbsp;true,&nbsp; "Id"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pagedate&nbsp;=&nbsp;ubll.
Page (1,&nbsp;20,&nbsp;out&nbsp;rows,&nbsp;null,&nbsp;true,&nbsp; "Num"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pagedate&nbsp;=&nbsp;ubll. Page (1,&nbsp;20,&nbsp;out&nbsp
rows,&nbsp;p&nbsp;=&gt;&nbsp;p.num&nbsp;&gt;&nbsp;1500,&nbsp;false,&nbsp; "Id"); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Get Quantity () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//without conditions &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;count&nbsp;=&nbsp;ubll.
GetCount (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//with conditions &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp; Count1&nbsp;=&nbsp;ubll.
GetCount (p&nbsp;=&gt;&nbsp;p.num&nbsp;&gt;&nbsp;5000); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Delete &nbsp;&nbsp;
&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Delete _ custom condition () &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Delete (p&nbsp;=&gt;&nbsp;p.num&nbsp;&gt;=&nbsp;2000); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Delete _ Delete model () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;model&nbsp;=&nbsp; Ubll.
FirstOrDefault (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; (model&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;UBLL.
Delete (model);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Delete _ Delete by ID () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;ubll.
Delete ("Ec45ea8b-a551-46eb-ad58-1b4f5f2aab25"); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Delete _ Remove all () &nbsp;&nbsp;&nbsp;&nbsp;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;UBLL.
DeleteAll (); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion &nbsp;&nbsp;&nbsp;&nbsp; #region &nbsp; Other &nbsp;&nbsp; &nbsp;&nbsp;[testmeThod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Create two objects at the same time _ in the same database () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logservices&nbsp;logbll&nbsp;=&nbsp;new&nbsp;logservices ();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userentity&nbsp;model&nbsp;=&nbsp;new&nbsp;userentity (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. username&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Num&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.
posttime&nbsp;=&nbsp;datetime.now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Insert (model);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logentity&nbsp;log&nbsp;=&nbsp;new&nbsp;logentity (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log. username1&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log. Num1&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbSp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.
posttime1&nbsp;=&nbsp;datetime.now;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logbll.insert (log); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. username&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Num&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.
posttime&nbsp;=&nbsp;datetime.now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Insert (model); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; Create two objects at the same time _ different database () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log1Services&nbsp;
Logbll&nbsp;=&nbsp;new&nbsp;log1services ();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userentity&nbsp;model&nbsp;=&nbsp;new&nbsp;userentity (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. username&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd. Next (100, &nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Num&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.
posttime&nbsp;=&nbsp;datetime.now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Insert (model);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logentity&nbsp;log&nbsp;=&nbsp;new&nbsp;logentity (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log. username1&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log. Num1&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.
posttime1&nbsp;=&nbsp;datetime.now;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logbll.insert (log); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. username&nbsp;=&nbsp; "Name" &nbsp;+&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model. Num&nbsp;=&nbsp;rd.
Next (100,&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbSp;model.
posttime&nbsp;=&nbsp;datetime.now; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ubll.
Insert (model); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;[testmethod] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp; When the specified name does not exist () &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errorservices&nbsp;error
&nbsp;=&nbsp;new&nbsp;errorservices (); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp; #endregion}


The above is their own encapsulation of the overall logic and code, but there are some do not understand and lack of place, here put forward, I hope the great God to help me answer:

1. Return value problem

In Tim, delete, change the use, according to the website provided by the driver, there is a Writeconcernresult object returned, but in the test found that this returned object is always null


2. Increase the ID problem

There is no mongodb in itself, with a objectid, if I need an ID, do I build one, and then I control +1 on the increase? However, this performance is relatively low, but also to consider the case of multithreading concurrency lock problem. So I don't know how this is going to happen.

3. Paging Efficiency issues

First page I was to turn the results into queryable, then in the operation, this code has this paragraph, temporarily commented out, the back of the blog to see a predecessor of the MongoDB analysis, changed the next page of the way, the test is very fast, but in the condition of the total number of records to get the time, found that the test 300W data, to get the total number of 600ms required time, I do not know whether the method is wrong or other better?




MongoDB Method and performance of paging query

This article focuses on MongoDB paging queries, why? Paging is a common number one killer, bad, the customer scold, the manager scolded.

Traditional SQL paging

Traditional SQL paging, all the scenarios are almost row_number, for the need for a variety of sorting, complex query scenarios, Row_number is the killer. In addition, for the current web is very popular Poll/push load paging, the time stamp is generally used to achieve pagination. These two kinds of pagination can be said that the former is universal, even LINQ generated paging is Row_number, it is conceivable that it more general. The latter is the best of both performance and complexity, as long as a simple timestamp can be.

MongoDB page

Enter into the MONGO of ideas, pagination is not difficult, what is rare? In fact, there is nothing, see understand also that way, and the idea of SQL paging is consistent.

First describes the use cases for this article, and I have imported the following Entity data in the database, where cus_id, amount I was born into an orderly number, the number of records poured is 200w:

public&nbsp;class&nbsp;test {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; primary Key &nbsp;ObjectId&nbsp; is MONGODB with its own primary key type &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public &nbsp;objectid&nbsp;id&nbsp;{&nbsp;get;&nbsp;set;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/// &nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Customer number &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[bsonelement ( "cust_id")] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;string&nbsp;CustomerId&nbsp;{&nbsp;get; &nbsp;set;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Total &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary &gt; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[bsonelement ("Amount")] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public&nbsp;int&nbsp;amount&nbsp;{&nbsp;get;&nbsp;set;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ &nbsp;&lt;summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///&nbsp; Status &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;///&nbsp;&lt;/summary&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[bsonelement ( "status")] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;string&nbsp;Status&nbsp;{&nbsp;get; &NBSP;SET;&NBSP}}




First take a look at the paging required parameters and results, the general paging required parameters are:





PageIndex Current Page


PageSize per page record number


Queryparam[] Other query fields





So according to Row_number's paging idea, that is, to take the first (pageindex*pagesize) to the (Pageindex*pagesize + pageSize), we use LINQ to express:





Query. Where (xxx...xxx). Skip (pageindex*pagesize). Take (PageSize)





Look up the data, there is a skip function, and there are limit functions see Resources 1, 2, so easy to implement such a paging query:





Db.test.find ({xxx...xxx}). Sort ({"Amount": 1}). Skip (a) limit (10)//Here ignore the query statement





Quite efficient, almost a few milliseconds out of the result, is really nosql efficiency first-class. But slow, the data I use here is only 10, and there is not much data. I add the data to 100000 and the efficiency is about 20ms. If this is the end of the study, it is really too much to live up to the spirit of the program ape to delve into. SQL Paging scheme, but there are a lot of, the efficiency is also different, that MONGO is this kind of, the answer is obviously not such. Is there a problem with performance in addition to efficiency? Redis in the article, have eaten such a loss, disorderly use the keys.





After reviewing some of the information, I found that all the information is as follows:


Do not easily use skip to do the query, otherwise the large amount of data will lead to a sharp decline in performance, this is because Skip is a one of the number over, more naturally slow.





So, skip is to avoid the use of, so how to avoid it? First of all, review the SQL page after a timestamp paging scheme, this use of the ordered nature of the field, using the query to fetch data, you can directly avoid a large number of digital. In other words, if you can attach such conditions to the query efficiency will improve, in fact, this is it? Let's verify:





Here we assume that the query 100,001th data, the amount value of this data is: 2399927, we write two statements, respectively, as follows:





Db.test.sort ({"Amount": 1}). Skip (100000). Limit (a)//183ms





Db.test.find ({amount:{$gt: 2399927}}). Sort ({"Amount": 1}). Limit (a)//53ms








The results have been attached to the annotations, and it is clear that the latter performance is One-third of the former, and the gap is very large. Also confirms the theory that skip efficiency is poor.





C # Implementation





We've already talked about the statement and efficiency of MongoDB paging, so let's implement the C # driver version.


The official Bson drive is used in this article, as detailed in reference 4. Mongo drive comes with another way one is similar to Ado.net's native query, one of which is LINQ, where both of us are implemented





Scenario One: Conditional Query native query implementation





var query = Query&lt;test&gt;. GT (item =&gt; item. Amount, 2399927);


var result = collection. Find (query). Setlimit (100)


. SetSortOrder (sortby.ascending ("Amount")). ToList ();


Console.WriteLine (Result. A (). Tojson ());//bson with Tojson





Program two: Skip native query implementation





var result = collection. FindAll (). Setskip (100000). Setlimit (100)


. SetSortOrder (sortby.ascending ("Amount"));


Console.WriteLine (Result. ToList (). A (). Tojson ());





Scenario Three: Linq condition query





var result = collection. Asqueryable&lt;test&gt; (). By (item =&gt; item). Amount)


. Where (item =&gt; item. Amount &gt; 2399927). Take (100);


Console.WriteLine (Result. A (). Tojson ());





Scenario Four: Linq Skip version





var result = collection. Asqueryable&lt;test&gt; (). By (item =&gt; item). Amount). Skip (100000). Take (100);


Console.WriteLine (Result. A (). Tojson ());





Performance Comparison Reference





The test code here later I upload, the specific implementation is to use the old Zhao (my idol ah ~) Codetimer to calculate performance. In addition, I run the code is used Testdriven Plug-ins to run.








Programme I:


Pagination Gt-limit
{"_id": ObjectId ("5472e383fc46de17c45d4682"), "cust_id": "A12399997", "Amount": 2399928, "STA Tus ":" B "} time
elapsed:1,322ms
CPU cycles:4,442,427,252
gen 0:0
Gen 1:0
G En 2:0



Programme II:

Pagination Skip-limit
{"_id": ObjectId ("5472e383fc46de17c45d4682"), "cust_id": "A12399997", "Amount": 2399928, "s Tatus ":" B "} time
elapsed:95ms
CPU cycles:18,280,728
gen 0:0
Gen 1:0
Gen 2 : 0



Programme III:

Paginatilinq on Linq where
{"_id": ObjectId ("5472e383fc46de17c45d4682"), "cust_id": "A12399997", "Amount": 2399928 , "status": "B"} time
elapsed:76ms
CPU cycles:268,734,988
gen 0:0
Gen 1:0
Gen 2:0



Programme IV:

Pagination Linq Skip
{"_id": ObjectId ("5472e383fc46de17c45d4682"), "cust_id": "A12399997", "Amount": 2399928, "STA         Tus ":" B "} time
elapsed:97ms
CPU cycles:30,834,648
Gen 0:0 Gen
1:0
Gen 2: 0


The result is not tumbling glasses, this and theory is too big, the first time why and behind the gap so big? At first I thought it was the driver of C # MONGO, trying to change the drive. These days I was watching "MongoDB in action" when I found the article mentioned:
MongoDB will load the index and metadata of the document into memory based on the query, and suggest that the size of the document metadata should always remain less than the machine memory, otherwise performance will decrease.

After noticing the above theory, I replaced my test plan, the first execution was excluded, and then compared, and found that the result was actually normal.

Revised results of programme I:

Pagination Gt-limit
{"_id": ObjectId ("5472e383fc46de17c45d4682"), "cust_id": "A12399997", "Amount
": 2399928 , "status": "B"} time
elapsed:18ms
CPU cycles:54,753,796
gen 0:0
Gen 1:0
  
   gen 2:0
  



Summarize

This article, based on the skip pagination and ordered field query paging two kinds of scenarios for comparison. The latter simply uses the results of the query to improve performance by not counting sequentially. Skip although less efficient but general some, ordered field query, need to design the page in the time to do some processing of this field, at least the number of points can get to this field. Here I attach a way, is the combination of the two, we can take each display of the page data on the last one, combined with skip to deal with pagination, so that is relatively better. There is no concrete implementation here. Other ways of performance comparison and implementation, welcome Daniel to share, thank you very much. In addition, if there are flaws and deficiencies in this article, please comment.

Related Article

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.