目标框架必须是 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();
}