目标框架必须是 4.5以上,最新MongoDb.Driver 是 2.2.4(与MongoDb.Driver 的1.x版本差别较大)

官方文档

http://mongodb.github.io/mongo-csharp-driver/2.2/

1)MongoDbContext类

internal class MongoDbContext
{
private readonly IMongoDatabase _database;

public MongoDbContext()
: this(
ConfigurationManager.AppSettings["MongoDbConnection"],
ConfigurationManager.AppSettings["MongoDbDatabase"])
{
}

public MongoDbContext(string connStr, string dbName)
{
if (string.IsNullOrEmpty(connStr) || string.IsNullOrEmpty(dbName))
throw new ArgumentNullException("MongoDbq连接信息有错误!");
var client = new MongoClient(connStr);
_database = client.GetDatabase(dbName);
}

public IMongoCollection<T> Collection<T>(string tableName="Activity")
{
return _database.GetCollection<T>(tableName);
}

}

2) 数据增删改查接口类

All the create, read, update, and delete (CRUD) operations take a similar form and are defined on theIMongoCollection<TDocument> interface.

public interface IMongoDbDao<T> where T : class,new()
{

/// <summary>
/// 获取一条记录
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
T GetOne(Expression<Func<T, bool>> whereLambda);

/// <summary>
/// 获取多条记录
/// </summary>
/// <returns></returns>
IEnumerable<T> GetList();

/// <summary>
/// 获取多条记录
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
IEnumerable<T> GetList(Expression<Func<T, bool>> whereLambda);

/// <summary>
/// 增加
/// </summary>
/// <param name="entity"></param>
void Insert(T entity);

/// <summary>
/// 批量增加
/// </summary>
/// <param name="entitys"></param>
void InsertMany(IEnumerable<T> entitys);

/// <summary>
/// 更新一个实体
/// </summary>
/// <param name="entity"></param>
void Update(T entity);

/// <summary>
/// 删除一个实体
/// </summary>
/// <param name="whereLambda"></param>
void Remove(Expression<Func<T, bool>> whereLambda);

/// <summary>
/// 统计数量
/// </summary>
/// <param name="whereLambda"></param>
long Count(Expression<Func<T, bool>> whereLambda);
}

3)MongoDb增删改查实现类

public class MongoDBService<T> : IMongoDbDao<T> where T : class,new()
{
MongoDbContext mc = new MongoDbContext();

/// <summary>
/// 查询符合条件的集合
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public IEnumerable<T> GetList( )
{
return mc.Collection<T>() .AsQueryable();

//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)
{
return mc.Collection<T>().Find(whereLambda).ToList();
//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)
{
return mc.Collection<T>().Find(whereLambda).FirstOrDefault();
//var query = GetList(whereLambda).FirstOrDefault();
//return query;
}

/// <summary>
/// 增加
/// </summary>
/// <param name="entity"></param>
public void Insert(T entity)
{

mc.Collection<T>().InsertOne(entity);
}

/// <summary>
/// 批量增加
/// </summary>
/// <param name="entitys"></param>
public void InsertMany(IEnumerable<T> entitys)
{
mc.Collection<T>().InsertMany( entitys);
}

/// <summary>
/// 更新一个实体
/// </summary>
/// <param name="entity"></param>
public void Update(T entity)
{
return;
// var filter = Builders<BsonDocument>.Filter.Eq("counter", 1);
//var updated = Builders<BsonDocument>.Update.Set("counter", 110);
//var result = collection.UpdateOneAsync(filter, updated).Result;
// Builders<BsonDocument>.Filter.Eq("")
// mc.Collection<T>().UpdateMany(whereLambda);

// mc.Collection<T>().UpdateOne(entity);
// mc.Collection<T>().Save(entity);
}

/// <summary>
/// 删除
/// </summary>
/// <param name="whereLambda"></param>
public void Remove(Expression<Func<T, bool>> whereLambda)
{
mc.Collection<T>().DeleteMany(whereLambda);
//var query = Query<T>.Where(whereLambda);
//mc.Collection<T>().Remove(query);
}

/// <summary>
/// 删除
/// </summary>
/// <param name="whereLambda"></param>
public long Count(Expression<Func<T, bool>> whereLambda)
{
//var query = Query<T>.Where(whereLambda);
return mc.Collection<T>().Count(whereLambda);
}
}

4)调用

static void Main(string[] args)
{
IMongoDbDao<BsonDocument> service=new MongoDBService<BsonDocument>();
var a= service.GetList();
Console.Read();
}

MongoDb学习1的更多相关文章

  1. MongoDB学习笔记系列

    回到占占推荐博客索引 该来的总会来的,Ef,Redis,MVC甚至Sqlserver都有了自己的系列,MongoDB没有理由不去整理一下,这个系列都是平时在项目开发时总结出来的,希望可以为各位一些帮助 ...

  2. MongoDB学习笔记—Linux下搭建MongoDB环境

    1.MongoDB简单说明 a MongoDB是由C++语言编写的一个基于分布式文件存储的开源数据库系统,它的目的在于为WEB应用提供可扩展的高性能数据存储解决方案. b MongoDB是一个介于关系 ...

  3. Mongodb学习笔记一(Mongodb环境配置)

    Mongodb学习 说明: MongoDB由databases组成,database由collections组成,collection由documents组成,document由fileds组成.Mo ...

  4. MongoDB学习记录

    一.操作符 "$lt" :"<""$lte" :"<=""$gt" :"> ...

  5. PHP操作MongoDB学习笔记

    <?php/*** PHP操作MongoDB学习笔记*///*************************//**   连接MongoDB数据库  **////*************** ...

  6. MongoDB学习:(二)MongoDB简单使用

    MongoDB学习:(二)MongoDB简单使用 MongoDB使用: 执行mongodb的操作之前,我们需要运行命令,来进入操作命令界面 >mongo 提示该错误,说明我们系统缺少一个补丁,该 ...

  7. MongoDB学习:(一)MongoDB安装

    MongoDB学习:(一)MongoDB安装 MongoDB介绍:     直接百科了: MongoDB安装: 1:下载安装: MongoDB安装:https://www.mongodb.com/do ...

  8. MongoDB学习(四)客户端工具备份数据库

    在上一篇MongoDB学习(三)中讲解了如何在服务器端进行数据的导入导出与备份恢复,本篇介绍下如何利用客户端工具来进行远程服务器的数据备份到本地. 以客户端工具MongoVUE为例来进行讲解: 1.首 ...

  9. mongodb学习(1) 第一次开启 mongdb

    1.启动mongdb 可以设置为开机启动 mongod -dbpath=/data/mongodb --fork --port 27017 --logpath=/usr/local/mongodb/l ...

  10. MongoDB学习之--安全和认证

    MongoDB学习之--安全和认证 本文主要介绍两部分内容,Mongodb的安全检查配置以及安全认证操作: 虽然确保系统安全是系统管理员的重要工作,但是作为程序员了解其机制也是大有好处的,毕竟不是每个 ...

随机推荐

  1. webpack入坑之旅(五)加载vue单文件组件

    这是一系列文章,此系列所有的练习都存在了我的github仓库中vue-webpack,在本人有了新的理解与认识之后,会对文章有不定时的更正与更新.下面是目前完成的列表: webpack入坑之旅(一)不 ...

  2. android 圆角图片的实现

    图片展示的时候总觉的直角的图片不好看?好办法来了!-- public class ToRoundCorner extends Activity{ public Bitmap toRoundCorner ...

  3. oracle数据泵实现不同用户之间的导出导入

    来源于:http://www.cnblogs.com/kevinsun/archive/2007/02/03/638803.aspx http://blog.sina.com.cn/s/blog_68 ...

  4. bzoj4403: 序列统计

    我们很容易发现答案是C(R-L+N+1,N)-1 然后用一下lucas定理就行了 #include <iostream> #include <cstdio> #include ...

  5. How to fix the sources list

    How to fix the sources list Sometimes the apt-get may not work, it is often caused by the misspelled ...

  6. bzoj 2005

    裸的2D gcd.ans=(Σ(d<=n)phi[d]*(n/d)*(m/d))*2-n*m; #include<iostream> #include<cstdio> # ...

  7. 【BZOJ-3626】LCA 树链剖分

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1428  Solved: 526[Submit][Status ...

  8. 【poj1017】 Packets

    http://poj.org/problem?id=1017 (题目链接) 题意 一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为1*1, 2*2, 3* ...

  9. bzoj1113: [Poi2008]海报PLA

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  10. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...