基于C#的MongoDB数据库开发应用(2)--MongoDB数据库的C#开发
<p>在上篇博客《基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用》里面,我总结了MongoDB数据库的一些基础信息,并在最后面部分简单介绍了数据库C#驱动的开发 ,本文继续这个主题,重点介绍MongoDB数据库C#方面的使用和封装处理过程,利用泛型和基类对象针对数据访问层进行的封装处理。</p><p>前面介绍到,当前2.2版本的数据库C#驱动的API,支持两种不同的开发接口,一个是基于MongoDatabase的对象接口,一个是IMongoDatabase的对象接口,前者中规中矩,和我们使用Shell里面的命令名称差不多;后者IMongoDatabase的接口是基于异步的,基本上和前者差别很大,而且接口都提供了异步的处理操作。</p>
<p>本文主要介绍基于MongoDatabase的对象接口的封装处理设置。</p>
<h3>1、数据访问层的设计</h3>
<p>在结合MongoDB数据库的C#驱动的特点,使用泛型和继承关系,把常规的处理接口做了抽象性的封装,以便封装适合更多业务的接口,减少子类代码及统一API的接口名称。</p>
<p>首先我们来看看大概的设计思路,我们把实体类抽象一个实体基类,方便使用。</p>
<p> <img src="https://images2015.cnblogs.com/blog/8867/201601/8867-20160106190845340-2014941491.png" alt="" width="216" height="230"></p>
<p>我们知道,在MongoDB数据库的集合里面,都要求文档有一个_id字段,这个是强制性的,而且这个字段的存储类型为ObjectId类型,这个值考虑了分布式的因素,综合了机器码,进程,时间戳等等方面的内容,它的构造如下所示。</p>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARIAAAAuCAIAAABrmgEWAAAWVklEQVR4nO2deVxM+//Hs0TkGtwsXWS7mYQUjSVNVOZWDOEypG4qS8uIiCi3q3DdylYelrK0oD0u1dwYya6aSmQptF7FpGhTMzXTOd8/Pg/ncX5nMzPmzl1+c/76nPf5nPe83q/P59lZZ9JISkpycXHhcDgcDmf79u11dXUwDL948SI5Obm1tRX6vLS1tXl4eBw+fLi7uxuGYSReVlaWkZEhkUggCIJhGNkEGsXFxQEBAdHR0ehdkG4SiSQ1NbW7uxsdl0qlYrG4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BwcGgf05OTlNTU1JSUlVV1R9//NHU1HTjxg3CgZFx+PHiybAJCgpis9lsNhuc6oD4gQMHnJycNm/ezGazwd/748ePs9lscMmenp4OoRbMp5NhA0EQ+OUGEI+KisrKykJWeTyeq6vr5cuXhUKhp6dnVlYW+J2AwsLCBw8eSKVSGIY7OzvFYrFYLG5vb29paZFIJBKJBHktWsY5Rt3Gb6KwHT86X69EOW8JNDY2JiYmgl/kAHHC/wwOvAOryJkYDMMSiQQ4LlephEqo2w0NDQMHDiTDpqurC61QlrR4JRTjR9ZGVru7ux0cHKD/u3R1dYH/Dg/+uTx1G6ySxdGb4uLi3NzcCFOBVerdlaJEro8QCoWJiYlJSUlJSUnV1dUSiQQ/ghT2Us8l6jmGb8uHjVAoPHfuHDhEgCU6OhpZlaWNXo3+vJB1o96dTMmRI0fYnxcPDw90/7CwsD59+gQGBrL/kcv8+fM1NDSsra2RCIvF0pBt0dTUpNFoNBqtV69effv27dmzp5aWFhL8jy1DhgyJjY3912DD4/H09fV1dXWnT5/u6empq6s7fvx4Lpfr5eXl5eXF5XK/2Eavcj8vZN2od0f3IftEb2/vs2fPcrnc1NTU7Ozs7Ozs8+fPh4SE3LhxI/u/tYBrGwiCCgoKKioqHjx48ObNm+fPn4MfVMAPP8UUUWwmqWCyQuR4/KOxwYhQ2E2llKp0JQqkwiuhKOovVaJE31SmRCm2o1OpTIkaGzU2xEWpQIlSbEenUpkSNTZqbIiLUoESpdiOTqUyJWps1NgQF6UCJUqxHZ1KZUr+ByCNR6NEKSDIAAAAAElFTkSuQmCC" alt=""></p>
<p>ObjectId是一个12字节的 BSON 类型字符串。按照字节顺序,依次代表:</p>
<ul>
<li>4字节:UNIX时间戳</li>
<li>3字节:表示运行MongoDB的机器</li>
<li>2字节:表示生成此_id的进程</li>
<li>3字节:由一个随机数开始的计数器生成的值</li>
</ul>
<p><img src="https://images2015.cnblogs.com/blog/8867/201601/8867-20160105093240950-2112739981.png" alt=""></p>
<p>实体基类BaseEntity包含了一个属性Id,这个是一个字符串型的对象(也可以使用ObjectId类型,但是为了方便,我们使用字符型,并声明为ObjectId类型即可),由于我们声明了该属性对象为ObjectId类型,那么我们就可以在C#代码里面使用字符串的ID类型了,代码如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> MongoDB实体类的基类
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> BaseEntity
{
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 基类对象的ID,MongoDB要求每个实体类必须有的主键
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"> </span>
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> Id { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
}</span></pre>
</div>
<p><span style="line-height: 1.5">然后利用泛型的方式,把数据访问层的接口提出来,并引入了数据访问层的基类进行实现和重用接口,如下所示。</span></p>
<p><img src="https://images2015.cnblogs.com/blog/8867/201601/8867-20160106191000934-243118883.png" alt="" width="401" height="322"></p>
<p>其中,上面几个类的定义如下所示。</p>
<p><img src="https://images2015.cnblogs.com/blog/8867/201601/8867-20160106191530418-1413781317.png" alt=""></p>
<p>数据访问层基类BaseDAL的类定义如下所示,主要就是针对上面的IBaseDAL<T>接口进行实现。</p>
<p><img src="https://images2015.cnblogs.com/blog/8867/201601/8867-20160106191819387-865077769.png" alt=""></p>
<p>有了这些基类的实现,我们对于实体类的处理就非常方便、统一了,基本上不需要在复制大量的代码来实现基础的增删改查分页实现了。</p>
<p>例如上面的User集合(表对象)的数据访问类定义如下所示,在对象的定义的时候,指定对应的实体类,并在构造函数里面指定对应的集合名称就可以实例化一个数据访问类了。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 数据表User对应的具体数据访问类
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> User : BaseDAL<UserInfo>, IBaseDAL<UserInfo><span style="color: rgba(0, 0, 0, 1)">
{
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 默认构造函数
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> User()
{
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.entitysName = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">users</span><span style="color: rgba(128, 0, 0, 1)">"</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对象在数据库的集合名称</span>
<span style="color: rgba(0, 0, 0, 1)"> }
.................</span></pre>
</div>
<h3>2、基类各种接口的实现</h3>
<p>前面小节我们介绍了实体基类,数据访问层基类接口和基类实现,以及具体集合对象的实现类的定义关系,通过泛型和继承关系,我们很好的抽象了各种对象的增删改查、分页等操作,子类继承了BaseDAL基类后,就自然而然的具有了非常强大的接口处理功能了。下面我们来继续详细介绍基于C#驱动的MongoDB数据库是如何进行各种增删改查等封装的。</p>
<p>1)构造MongoDatabase对象</p>
<p>首先我们需要利用连接字符串来构建<span style="line-height: 16.8px">MongoDatabase对象,因为所有的接口都是基于这个对象进行处理的,代码如下所示。</span></p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据数据库配置信息创建MongoDatabase对象,如果不指定配置信息,则从默认信息创建
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="databaseName"></span><span style="color: rgba(0, 128, 0, 1)">数据库名称,默认空为local</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span><span style="color: rgba(0, 0, 0, 1)"> MongoDatabase CreateDatabase()
{
</span><span style="color: rgba(0, 0, 255, 1)">string</span> connectionString = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.IsNullOrEmpty(dbConfigName))
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从配置文件中获取对应的连接信息</span>
connectionString =<span style="color: rgba(0, 0, 0, 1)"> ConfigurationManager.ConnectionStrings.ConnectionString;
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
{
connectionString </span>=<span style="color: rgba(0, 0, 0, 1)"> defaultConnectionString;
}
</span><span style="color: rgba(0, 0, 255, 1)">var</span> client = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient(connectionString);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> database = client.GetServer().GetDatabase(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MongoUrl(connectionString).DatabaseName);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> database;
}</span></pre>
</div>
<p>2)构建MongoCollection对象</p>
<p>上面构建了MongoDatabase对象后,我们需要基于这个基础上再创建一个对象的MongoCollection对象,这个就是类似我们关系数据库里面的表对象的原型了。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 获取操作对象的MongoCollection集合,强类型对象集合
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> MongoCollection<T><span style="color: rgba(0, 0, 0, 1)"> GetCollection()
{
MongoDatabase database </span>=<span style="color: rgba(0, 0, 0, 1)"> CreateDatabase();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> database.GetCollection<T>(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.entitysName);
}</span></pre>
</div>
<p>3)查询单个对象</p>
<p>利用MongoCollection对象,我们可以通过API接口获取对应的对象,单个对象的接口为FindOneById(也可以用FindOne接口,如注释部分的代码),我们具体的处理代码如下所示</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 查询数据库,检查是否存在指定ID的对象
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="key"></span><span style="color: rgba(0, 128, 0, 1)">对象的ID值</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">存在则返回指定的对象,否则返回Null</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> T FindByID(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> id)
{
ArgumentValidation.CheckForEmptyString(id, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象id为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> collection.FindOneById(<span style="color: rgba(0, 0, 255, 1)">new</span> ObjectId(id)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">FindOne(Query.EQ("_id", new ObjectId(id)));</span>
}</pre>
</div>
<p>如果基于条件的单个记录查询,我们可以使用Expression<Func<T, bool>>和IMongoQuery的参数进行处理,如下代码所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件查询数据库,如果存在返回第一个对象
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="match"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">存在则返回指定的第一个对象,否则返回默认值</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> T FindSingle(Expression<Func<T, <span style="color: rgba(0, 0, 255, 1)">bool</span>>><span style="color: rgba(0, 0, 0, 1)"> match)
{
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> collection.AsQueryable().Where(match).FirstOrDefault();
}
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件查询数据库,如果存在返回第一个对象
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="query"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">存在则返回指定的第一个对象,否则返回默认值</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span><span style="color: rgba(0, 0, 0, 1)"> T FindSingle(IMongoQuery query)
{
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> collection.FindOne(query);
} </span></pre>
</div>
<p>4)IQueryable的接口利用</p>
<p>使用过EF的实体框架的话,我们对其中的IQueryable<T>印象很深刻,它可以给我提供很好的LINQ语法获取对应的信息,它可以通过使用Expression<Func<T, bool>>和IMongoQuery的参数来进行条件的查询操作,MongoCollection对象有一个AsQueryable()的API进行转换,如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 返回可查询的记录源
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> IQueryable<T><span style="color: rgba(0, 0, 0, 1)"> GetQueryable()
{
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
IQueryable</span><T> query =<span style="color: rgba(0, 0, 0, 1)"> collection.AsQueryable();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> query.OrderBy(<span style="color: rgba(0, 0, 255, 1)">this</span>.SortPropertyName, <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.IsDescending);
}</span></pre>
</div>
<p>如果是通过使用Expression<Func<T, bool>>和IMongoQuery的参数,那么处理的接口代码如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件表达式返回可查询的记录源
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="match"></span><span style="color: rgba(0, 128, 0, 1)">查询条件</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="sortPropertyName"></span><span style="color: rgba(0, 128, 0, 1)">排序表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="isDescending"></span><span style="color: rgba(0, 128, 0, 1)">如果为true则为降序,否则为升序</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> IQueryable<T> GetQueryable(Expression<Func<T, <span style="color: rgba(0, 0, 255, 1)">bool</span>>> match, <span style="color: rgba(0, 0, 255, 1)">string</span> sortPropertyName, <span style="color: rgba(0, 0, 255, 1)">bool</span> isDescending = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
{
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
IQueryable</span><T> query =<span style="color: rgba(0, 0, 0, 1)"> collection.AsQueryable();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (match != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
query </span>=<span style="color: rgba(0, 0, 0, 1)"> query.Where(match);
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> query.OrderBy(sortPropertyName, isDescending);
}</span></pre>
</div>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件表达式返回可查询的记录源
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="query"></span><span style="color: rgba(0, 128, 0, 1)">查询条件</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="sortPropertyName"></span><span style="color: rgba(0, 128, 0, 1)">排序表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="isDescending"></span><span style="color: rgba(0, 128, 0, 1)">如果为true则为降序,否则为升序</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> IQueryable<T> GetQueryable(IMongoQuery query, <span style="color: rgba(0, 0, 255, 1)">string</span> sortPropertyName, <span style="color: rgba(0, 0, 255, 1)">bool</span> isDescending = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
{
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
IQueryable</span><T> queryable =<span style="color: rgba(0, 0, 0, 1)"> collection.Find(query).AsQueryable();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> queryable.OrderBy(sortPropertyName, isDescending);
}</span></pre>
</div>
<p>5)集合的查询处理</p>
<p>通过利用上面的IQueryable<T>对象,以及使用Expression<Func<T, bool>>和IMongoQuery的参数,我们很好的进行集合的查询处理操作的了,具体代码如下所示</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件查询数据库,并返回对象集合
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="match"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">指定对象的集合</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> IList<T> Find(Expression<Func<T, <span style="color: rgba(0, 0, 255, 1)">bool</span>>><span style="color: rgba(0, 0, 0, 1)"> match)
{
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> GetQueryable(match).ToList();
}
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件查询数据库,并返回对象集合
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="query"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">指定对象的集合</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> IList<T><span style="color: rgba(0, 0, 0, 1)"> Find(IMongoQuery query)
{
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> collection.Find(query).ToList();
}</span></pre>
</div>
<p>对于分页,我们是非常需要的,首先在大数据的集合里面,我们不可能一股脑的把所有的数据全部返回,因此根据分页参数返回有限数量的集合处理就是我们应该做的,分页的操作代码和上面很类似,只是利用了Skip和Take的接口,返回我们需要的记录数量就可以了。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件查询数据库,并返回对象集合(用于分页数据显示)
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="match"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="info"></span><span style="color: rgba(0, 128, 0, 1)">分页实体</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">指定对象的集合</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> IList<T> FindWithPager(Expression<Func<T, <span style="color: rgba(0, 0, 255, 1)">bool</span>>><span style="color: rgba(0, 0, 0, 1)"> match, PagerInfo info)
{
</span><span style="color: rgba(0, 0, 255, 1)">int</span> pageindex = (info.CurrenetPageIndex < <span style="color: rgba(128, 0, 128, 1)">1</span>) ? <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)"> : info.CurrenetPageIndex;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> pageSize = (info.PageSize <= <span style="color: rgba(128, 0, 128, 1)">0</span>) ? <span style="color: rgba(128, 0, 128, 1)">20</span><span style="color: rgba(0, 0, 0, 1)"> : info.PageSize;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> excludedRows = (pageindex - <span style="color: rgba(128, 0, 128, 1)">1</span>) *<span style="color: rgba(0, 0, 0, 1)"> pageSize;
IQueryable</span><T> query =<span style="color: rgba(0, 0, 0, 1)"> GetQueryable(match);
info.RecordCount </span>=<span style="color: rgba(0, 0, 0, 1)"> query.Count();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> query.<span style="color: rgba(255, 0, 0, 1)"><strong>Skip</strong></span>(excludedRows).<span style="color: rgba(255, 0, 0, 1)"><strong>Take</strong></span>(pageSize).ToList();
}</span></pre>
</div>
<p>或者是下面的代码</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据条件查询数据库,并返回对象集合(用于分页数据显示)
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="query"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="info"></span><span style="color: rgba(0, 128, 0, 1)">分页实体</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">指定对象的集合</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> IList<T><span style="color: rgba(0, 0, 0, 1)"> FindWithPager(IMongoQuery query, PagerInfo info)
{
</span><span style="color: rgba(0, 0, 255, 1)">int</span> pageindex = (info.CurrenetPageIndex < <span style="color: rgba(128, 0, 128, 1)">1</span>) ? <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)"> : info.CurrenetPageIndex;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> pageSize = (info.PageSize <= <span style="color: rgba(128, 0, 128, 1)">0</span>) ? <span style="color: rgba(128, 0, 128, 1)">20</span><span style="color: rgba(0, 0, 0, 1)"> : info.PageSize;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> excludedRows = (pageindex - <span style="color: rgba(128, 0, 128, 1)">1</span>) *<span style="color: rgba(0, 0, 0, 1)"> pageSize;
IQueryable</span><T> queryable =<span style="color: rgba(0, 0, 0, 1)"> GetQueryable(query);
info.RecordCount </span>=<span style="color: rgba(0, 0, 0, 1)"> queryable.Count();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> queryable.<strong><span style="color: rgba(255, 0, 0, 1)">Skip</span></strong>(excludedRows).<strong><span style="color: rgba(255, 0, 0, 1)">Take</span></strong>(pageSize).ToList();
}</span></pre>
</div>
<p>6)对象的写入操作</p>
<p>对象的写入可以使用save,它是根据_id的来决定插入还是更新的,如下代码所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 保存指定对象到数据库中,根据Id的值,决定是插入还是更新
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="t"></span><span style="color: rgba(0, 128, 0, 1)">指定的对象</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功指定对象信息</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span><span style="color: rgba(0, 0, 0, 1)"> T Save(T t)
{
ArgumentValidation.CheckForNullReference(t, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象t为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.Save(t);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> t;
}</span></pre>
</div>
<p>插入记录就可以利用insert方法进行处理的,代码如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 插入指定对象到数据库中
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="t"></span><span style="color: rgba(0, 128, 0, 1)">指定的对象</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功返回</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">true</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">,否则为</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">false</span><span style="color: rgba(128, 128, 128, 1)"></c></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span><span style="color: rgba(0, 0, 0, 1)"> Insert(T t)
{
ArgumentValidation.CheckForNullReference(t, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象t为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.Insert(t);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result != <span style="color: rgba(0, 0, 255, 1)">null</span> && result.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>如果是批量插入,可以利用它的insertBatch的方法进行处理,具体代码如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 插入指定对象集合到数据库中
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="list"></span><span style="color: rgba(0, 128, 0, 1)">指定的对象集合</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功返回</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">true</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">,否则为</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">false</span><span style="color: rgba(128, 128, 128, 1)"></c></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> InsertBatch(IEnumerable<T><span style="color: rgba(0, 0, 0, 1)"> list)
{
ArgumentValidation.CheckForNullReference(list, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象list为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.InsertBatch(list);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result.Any(s => s != <span style="color: rgba(0, 0, 255, 1)">null</span> && s.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">部分成功也返回true</span>
}</pre>
</div>
<p>7)对象的更新操作</p>
<p>更新操作分为了两个不同的部分,一个是全部的记录更新,也就是整个JSON的替换操作了,一般我们是在原来的基础上进行更新的,如下代码所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 更新对象属性到数据库中
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="t"></span><span style="color: rgba(0, 128, 0, 1)">指定的对象</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="id"></span><span style="color: rgba(0, 128, 0, 1)">主键的值</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功返回</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">true</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">,否则为</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">false</span><span style="color: rgba(128, 128, 128, 1)"></c></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> Update(T t, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> id)
{
ArgumentValidation.CheckForNullReference(t, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象t为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
ArgumentValidation.CheckForEmptyString(id, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象id为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">bool</span> result = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> existing =<span style="color: rgba(0, 0, 0, 1)"> FindByID(id);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (existing != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(0, 0, 255, 1)">var</span> resultTmp =<span style="color: rgba(0, 0, 0, 1)"> collection.Save(t);
result </span>= resultTmp != <span style="color: rgba(0, 0, 255, 1)">null</span> && resultTmp.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result;
}</span></pre>
</div>
<p>还有一种方式是部分更新,也就是更新里面的指定一个或几个字段,不会影响其他字段,也就不会全部替换掉其他内容的操作了。这里利用了一个UpdateBuilder<T>的对象,用来指定那些字段需要更新,以及这些字段的值内容的,具体的更新代码如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 封装处理更新的操作
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="id"></span><span style="color: rgba(0, 128, 0, 1)">主键的值</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="update"></span><span style="color: rgba(0, 128, 0, 1)">更新对象</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功返回</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">true</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">,否则为</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">false</span><span style="color: rgba(128, 128, 128, 1)"></c></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> Update(<span style="color: rgba(0, 0, 255, 1)">string</span> id, UpdateBuilder<T><span style="color: rgba(0, 0, 0, 1)"> update)
{
ArgumentValidation.CheckForNullReference(update, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象update为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
ArgumentValidation.CheckForEmptyString(id, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象id为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> query = Query.EQ(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_id</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId(id));
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.Update(query, update);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result != <span style="color: rgba(0, 0, 255, 1)">null</span> && result.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>部分更新,可以结合使用Inc和Set方法来进行处理,如下是我在子类里面利用到上面的Update部分更新的API进行处理个别字段的更新操作。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 为用户增加岁数
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="id"></span><span style="color: rgba(0, 128, 0, 1)">记录ID</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="addAge"></span><span style="color: rgba(0, 128, 0, 1)">待增加的岁数</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> IncreaseAge(<span style="color: rgba(0, 0, 255, 1)">string</span> id, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> addAge)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">增加指定的岁数</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> query = Query<UserInfo>.EQ(s =><span style="color: rgba(0, 0, 0, 1)"> s.Id, id);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> update = Update<UserInfo>.Inc(s =><span style="color: rgba(0, 0, 0, 1)"> s.Age, addAge);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.Update(query, update);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result != <span style="color: rgba(0, 0, 255, 1)">null</span> && result.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 单独修改用户的名称
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="id"></span><span style="color: rgba(0, 128, 0, 1)">记录ID</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="newName"></span><span style="color: rgba(0, 128, 0, 1)">用户新名称</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> UpdateName(<span style="color: rgba(0, 0, 255, 1)">string</span> id, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> newName)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">增加指定的岁数</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> query = Query<UserInfo>.EQ(s =><span style="color: rgba(0, 0, 0, 1)"> s.Id, id);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> update = Update<UserInfo>.Set(s =><span style="color: rgba(0, 0, 0, 1)"> s.Name, newName);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.Update(query, update);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result != <span style="color: rgba(0, 0, 255, 1)">null</span> && result.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>8)对象的删除操作</p>
<p>对象的删除,一般可以利用条件进行删除,如单个删除可以使用_id属性进行处理,也可以利用批量删除的接口进行删除操作,代码如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据指定对象的ID,从数据库中删除指定对象
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="id"></span><span style="color: rgba(0, 128, 0, 1)">对象的ID</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功返回</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">true</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">,否则为</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">false</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">。</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> Delete(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> id)
{
ArgumentValidation.CheckForEmptyString(id, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象id为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">var result = collection.Remove(Query<T>.EQ(s => s.Id, id));</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> result = collection.Remove(Query.EQ(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_id</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId(id)));
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result != <span style="color: rgba(0, 0, 255, 1)">null</span> && result.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>其中上面注释的var result = collection.Remove(Query<T>.EQ(s => s.Id, id));代码,就是利用了强类型的对象属性和值进行移除,一样可以的。</p>
<p>对于批量删除,可以利用Query的不同进行处理。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据指定对象的ID,从数据库中删除指定指定的对象
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="idList"></span><span style="color: rgba(0, 128, 0, 1)">对象的ID集合</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功返回</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">true</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">,否则为</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">false</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">。</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> DeleteBatch(List<<span style="color: rgba(0, 0, 255, 1)">string</span>><span style="color: rgba(0, 0, 0, 1)"> idList)
{
ArgumentValidation.CheckForNullReference(idList, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">传入的对象idList为空</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> query = Query.In(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_id</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BsonArray(idList));
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.Remove(query);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result != <span style="color: rgba(0, 0, 255, 1)">null</span> && result.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>或者基于IMongoQuery的条件进行处理。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据指定条件,从数据库中删除指定对象
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="match"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></span><span style="color: rgba(0, 128, 0, 1)">执行成功返回</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">true</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">,否则为</span><span style="color: rgba(128, 128, 128, 1)"><c></span><span style="color: rgba(0, 128, 0, 1)">false</span><span style="color: rgba(128, 128, 128, 1)"></c></span><span style="color: rgba(0, 128, 0, 1)">。</span><span style="color: rgba(128, 128, 128, 1)"></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span><span style="color: rgba(0, 0, 0, 1)"> DeleteByQuery(IMongoQuery query)
{
MongoCollection</span><T> collection =<span style="color: rgba(0, 0, 0, 1)"> GetCollection();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> collection.Remove(query);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result != <span style="color: rgba(0, 0, 255, 1)">null</span> && result.DocumentsAffected > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
} </span></pre>
</div>
<p>9)其他相关接口</p>
<p>一般除了上面的接口,还有一些其他的接口,如获取记录的总数、判断条件的记录是否存在等也是很常见的,他们的代码封装如下所示。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 获取表的所有记录数量
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> GetRecordCount()
{
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> GetQueryable().Count();
}</span></pre>
</div>
<div class="cnblogs_code">
<pre> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据查询条件,判断是否存在记录
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="match"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> IsExistRecord(Expression<Func<T, <span style="color: rgba(0, 0, 255, 1)">bool</span>>><span style="color: rgba(0, 0, 0, 1)"> match)
{
</span><span style="color: rgba(0, 0, 255, 1)">return</span> GetQueryable(match).Any();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">.Count() > 0</span>
<span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 根据查询条件,判断是否存在记录
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="query"></span><span style="color: rgba(0, 128, 0, 1)">条件表达式</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">bool</span><span style="color: rgba(0, 0, 0, 1)"> IsExistRecord(IMongoQuery query)
{
</span><span style="color: rgba(0, 0, 255, 1)">return</span> GetQueryable(query).Any();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">.Count() > 0</span>
}</pre>
</div>
<p> </p>
<p>非常感谢您的详细阅读,以上基本上就是我对整个MongoDB数据库的各个接口的基类封装处理了,其中已经覆盖到了基础的增删改查、分页等操作接口,以及一些特殊的条件处理接口的扩展,我们利用这些封装好的基类很好的简化了子类的代码,而且可以更方便的在基类的基础上进行特殊功能的扩展处理。</p>
<p>当然,以上介绍的都不是最新的接口,是2.0(或2.2)版本之前的接口实现,虽然在2.2里面也还可以利用上面的MongoDatabase对象接口,但是IMongoDatabase最新的接口已经全面兼容异步的操作,但也是一个很大的跳跃,基本上引入了不同的接口命名和处理方式,利用异步可以支持更好的处理体验,但是也基本上是需要对所有的接口进行了全部的重写了。</p>
<p>下一篇我会专门介绍一下基于最新的异步接口如何实现这些常规增删改查、分页等的基类实现封装处理。</p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 98%; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee;">
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt>
<span style="color: #000000"><span class="Apple-tab-span" style="white-space: pre"></span>
专注于代码生成工具、.Net/Python 框架架构及软件开发,以及各种Vue.js的前端技术应用。著有Winform开发框架/混合式开发框架、微信开发框架、Bootstrap开发框架、ABP开发框架、SqlSugar开发框架、Python开发框架等框架产品。
<br> 转载请注明出处:撰写人:伍华聪 http://www.iqidi.com <br> </span></div><br><br>
来源:https://www.cnblogs.com/wuhuacong/p/5106662.html
頁:
[1]