MongoDB 工具助手类(.NET)
在开发过程中,需要用到MongoDB,本身MongoDB自己对类的封装就特别好了。为了更加符合我们平时的开发使用,我现在进行了一个简单的封装操作。
连接数据库类:MongoDBContext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration; using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq; namespace XXXXX.MongoDB
{
public class MongoDBContext
{
// 数据库链接
private readonly MongoDatabase database; public MongoDBContext()
: this(ConfigurationManager.AppSettings["DefaultMongoDBConnection"], ConfigurationManager.AppSettings["DefaultMonoDbDatabase"])
{
} /// <summary>
/// 构造函数。根据指定连接字符串和数据库名
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="dbName">数据库名</param>
public MongoDBContext(string connectionString, string dbName)
{
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException("connectionString is null");
} if (string.IsNullOrEmpty(dbName))
{
throw new ArgumentNullException("dbName is null");
} var client = new MongoClient(connectionString);
var server = client.GetServer();
this.database = server.GetDatabase(dbName);
} /// <summary>
/// 获取当前连接数据库的指定集合【依据类型】
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public MongoCollection<T> Collection<T>()
{
return database.GetCollection<T>(typeof(T).Name);
}
}
}
服务操作类:MongoDBService
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; namespace XXXXX.MongoDB
{ public class MongoDBService<T> : IMongoDBService<T> where T : class,new()
{
MongoDBContext mc = new MongoDBContext(); /// <summary>
/// 查询符合条件的集合
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public IEnumerable<T> GetList()
{
var query = Query<T>.Where(o => true);
return mc.Collection<T>().Find(query);
} /// <summary>
/// 查询符合条件的集合
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public IEnumerable<T> GetList(Expression<Func<T, bool>> whereLambda)
{
var query = Query<T>.Where(whereLambda);
return mc.Collection<T>().Find(query);
} /// <summary>
/// 查询一条记录
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public T GetOne(Expression<Func<T, bool>> whereLambda)
{
var query = GetList(whereLambda).FirstOrDefault();
return query;
} /// <summary>
/// 增加
/// </summary>
/// <param name="entity"></param>
public void Insert(T entity)
{
mc.Collection<T>().Insert(entity);
} /// <summary>
/// 批量增加
/// </summary>
/// <param name="entitys"></param>
public void InsertAll(IEnumerable<T> entitys)
{
mc.Collection<T>().InsertBatch(entitys);
} /// <summary>
/// 更新一个实体
/// </summary>
/// <param name="entity"></param>
public void Update(T entity)
{
mc.Collection<T>().Save(entity);
} /// <summary>
/// 删除
/// </summary>
/// <param name="whereLambda"></param>
public void Remove(Expression<Func<T, bool>> whereLambda)
{
var query = Query<T>.Where(whereLambda);
mc.Collection<T>().Remove(query);
}
}
}
上面方法封装完后,我们就可以直接使用了。使用很简单,比如,我有一个ActivityModel实体,使用时,如下:
public void activityTest()
{
var activityDb = new MongoDBService<ActivityModel>();
var activityList = activityDb.GetList().ToList();
var activity = activityDb.GetOne(o => o.Id==ObjectId.Parse("54d9aecd89f0bd14d81a63a7"));
var xxx = activity.To();
}
MongoDB 工具助手类(.NET)的更多相关文章
- JAVA单例MongoDB工具类
我经常对MongoDB进行一些基础操作,将这些常用操作合并到一个工具类中,方便自己开发使用. 没用Spring Data.Morphia等框架是为了减少学习.维护成本,另外自己直接JDBC方式的话可以 ...
- mongodb工具类
pom.xml文件增加Mongodb jar包 <dependency> <groupId>org.mongodb</groupId> <artifactId ...
- ADO.NET数据库操作助手类
SQL语句操作增删查改助手类 using System; using System.Collections.Generic; using System.Configuration; using Sys ...
- 【C#】SQL数据库助手类2.0(自用)
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- AES加密解密 助手类 CBC加密模式
"; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...
- Yii2 数组助手类arrayHelper
数组助手类 ArrayHelper 1.什么是数组助手类 Yii 数组助手类提供了额外的静态方法,让你更高效的处理数组. a.获取值(getValue) class User { public $na ...
- Linux性能优化 第八章 实用工具:性能工具助手
8.1性能工具助手 Linux有丰富的工具,这些工具组合来使用会更加强大.性能工具也一样,单独使用虽然也没有问题,但是和其他的工具组合起来就能显著提高有效性和易用性. 8.1.1 自动执行和记录命令 ...
- WorldWind源码剖析系列:代理助手类ProxyHelper
代理助手类ProxyHelper通过平台调用的互操作技术封送了若干Win32结构体和函数.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: 内嵌类型WINHTTP_AUTOPROX ...
- WorldWind源码剖析系列:图像助手类ImageHelper
图像助手类ImageHelper封装了对各种图像的操作.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: public static bool IsGdiSupportedImag ...
随机推荐
- 二分法求平方根(Python实现)
使用二分法(Bisection Method)求平方根. def sqrtBI(x, epsilon): assert x>0, 'X must be non-nagtive, not ' + ...
- HTML5 css3 阴影效果
阴影效果曾让 Web 设计师既爱又恨,现在,有了 CSS3,你不再需要 Photoshop,已经有网站在使用这个功能了,如 24 Ways website. -webkit-box-shadow: 1 ...
- jQuery弹出层插件大全
1.thickbox 目前用的比较多的,最新版本是thickbox3.1 下载地址:http://jquery.com/demo/thickbox/#examples 2.colorBox 官方网站: ...
- XSS 跨站脚本攻击(Cross Site Scripting)
xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/修改/删除数据的目的,而在xss攻击中,通过插入恶意 ...
- 学习项目部署Django+uwsgi+Nginx生产环境部署
绪论 项目生产环境部署,是很重的一个知识点.第一,Django自带的服务器很简陋,由于测试和开发环境尚可,无法用于生产环境,保障安全性和可靠性.以及性能.此外,学习部署方式,还有利于了解生产部署后的项 ...
- Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitmapinfo' (aka) 'enum CGBitmapInfo')
The constants for specifying the alpha channel information are declared with the CGImageAlphaInfo ty ...
- linux常用命令之scp详解
使用scp的前提: 1.服务端启动了sshd服务 2.是本地和远程两端的系统都必须要有scp这个命令.即openssh-clients软件包 [安装方法] [root@ ~]# yum install ...
- JAVA unicode转换成中文
/** * * unicode 转换成 中文 * @param theString * @return */ public static String decodeUnicode(String the ...
- Node学习HTTP模块(HTTP 服务器与客户端)
Node学习HTTP模块(HTTP 服务器与客户端) Node.js 标准库提供了 http 模块,其中封装了一个高效的 HTTP 服务器和一个简易的HTTP 客户端.http.Server 是一个基 ...
- java try catch 异常后还会继续执行吗
catch 中如果你没有再抛出异常 , 那么catch之后的代码是可以继续执行的 , 但是try中 , 报错的那一行代码之后 一直到try结束为止的这一段代码 , 是不会再执行的. ========= ...