查增改删
链接字符串 MongoDB超管+(admin) 单独库用户不加
static string mongoR = string.Format("mongodb://{0}(admin):{1}@{2}:{3}", "MongoRead", "123456", "127.0.0.1", 27017);
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver; namespace MongoTest
{
public class EMongoModel
{
/// <summary>
/// 连接字符串
/// </summary>
public string ConnStr { get; set; }
/// <summary>
/// 数据库名称
/// </summary>
public string DBName { get; set; }
/// <summary>
/// 数据库表名称
/// </summary>
public string CollName { get; set; }
}
public class EMongo
{
#region 查询
public static T Select<T>(EMongoModel mongoM, IMongoQuery query = null) where T : class,new()
{
T t = null;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
t = dbCollection.FindOneAs<T>(query);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return t;
}
public static List<T> SelectList<T>(EMongoModel mongoM, IMongoQuery query = null, IMongoSortBy sort = null, params string[] fields) where T : class,new()
{
List<T> ls = null;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
MongoCursor<T> cursor = null;
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
cursor = dbCollection.FindAs<T>(query);
if (sort != null) { cursor.SetSortOrder(sort); }
if (fields != null) { cursor.SetFields(fields); }
}
ls = cursor.ToList<T>();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ls;
}
public static List<T> SelectListPage<T>(EMongoModel mongoM, int pageIndex, int pageSize, out long rowCount, IMongoQuery query = null, IMongoSortBy sort = null, params string[] fields) where T : class,new()
{
List<T> ls = null;
rowCount = ;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
MongoCursor<T> cursor = null;
int startIndex = (pageIndex - ) * pageSize;
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
cursor = dbCollection.FindAs<T>(query);
cursor.SetSkip(startIndex).SetLimit(pageSize);
rowCount = cursor.Count();
if (sort != null) { cursor.SetSortOrder(sort); }
if (fields != null && fields.Length > ) { cursor.SetFields(fields); }
}
ls = cursor.ToList<T>();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ls;
}
#endregion #region 添加
public static bool Insert<T>(EMongoModel mongoM, T model) where T : class,new()
{
bool ret = false;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
ret = dbCollection.Insert(model).Ok;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ret;
}
public static bool Insert<T>(EMongoModel mongoM, List<T> modelLs) where T : class,new()
{
bool ret = false;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
IEnumerable<WriteConcernResult> result = null;
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
result = dbCollection.InsertBatch(modelLs);
}
ret = result.Where(x => x.Ok).Count() > ;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ret;
}
#endregion #region 修改
public static bool Update<T>(EMongoModel mongoM, T model) where T : class,new()
{
bool ret = false;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
ret = dbCollection.Save<T>(model).Ok;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ret;
}
public static bool Update(EMongoModel mongoM, IMongoUpdate update, IMongoQuery query = null)
{
bool ret = false;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
ret = dbCollection.Update(query, update, UpdateFlags.Multi).Ok;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ret;
}
#endregion #region 删除
public static bool Delete(EMongoModel mongoM, IMongoQuery query)
{
bool ret = false;
try
{
MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
MongoDatabase db = server.GetDatabase(mongoM.DBName);
using (server.RequestStart(db))
{
MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
ret = dbCollection.Remove(query).Ok;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return ret;
}
#endregion
}
}