查增改删

链接字符串 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
}
}

MongoDB-C#驱动帮助的更多相关文章

  1. (转载)MongoDB C#驱动中Query几个方法

    MongoDB C#驱动中Query几个方法 Query.All("name", "a", "b");//通过多个元素来匹配数组 Query ...

  2. 使用VS2010编译MongoDB C++驱动详解

    最近为了解决IM消息记录的高速度写入.多文档类型支持的需求,决定使用MongoDB来解决. 考虑到MongoDB对VS版本要求较高,与我现有的VS版本不兼容,在leveldb.ssdb.redis.h ...

  3. MongoDB C#驱动:

    MongoDB C#驱动: http://xiaosheng.me/2016/09/15/article24 http://www.cnblogs.com/wuhuacong/p/5098348.ht ...

  4. MongoDB C#驱动

    烟波钓徒 MongoDB C#驱动 http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial 笔记 首先下载驱动.驱动有两个文件 Mongo ...

  5. [转载]MongoDB C# 驱动教程

    本教程基于C#驱动 v1.6.x . Api 文档见此处: http://api.mongodb.org/csharp/current/. 简介 本教程介绍由10gen支持的,用于MongoDB的C# ...

  6. MongoDB操作(1)—MongoDB java驱动核心层次结构及操作流程

    MongoDB之java驱动学习 预备: 本地运行MongoDB采用默认端口20717: 安装MongoDB驱动: 以下关键步骤. 核心层次结构或步骤: 创建连接池:MongoClient实例. 对于 ...

  7. MongoDB C# 驱动教程

    C# 驱动版本 v1.6.x 本教程基于C#驱动 v1.6.x . Api 文档见此处: http://api.mongodb.org/csharp/current/. 简介 本教程介绍由10gen支 ...

  8. [转]MongoDB c++驱动安装与使用

    安装 获取源码:git clone https://github.com/mongodb/mongo-cxx-driver.git,解压 安装编译工具scons:yum install -y scon ...

  9. [原创]MongoDB C++ 驱动部分问题解决方案(MongoDB C++ Driver)

    本文为我长时间开发以及修改MongoDB C++ Driver时的一些问题和解决方案.目前本文所介绍的相关引擎也已经发布闭源版本,请自行下载 库版本以及相关位置:http://code.google. ...

  10. MongoDb C# 驱动操作示例

    c#操作mongo数据库 驱动采用http://www.oschina.net/p/mongo-csharp-driver C#驱动的基本数据库连接,增删改查操作 //定义对象 public clas ...

随机推荐

  1. EF唯一索引

    this.Property(p => p.Name) .IsRequired() .HasMaxLength()) .HasColumnAnnotation("Index", ...

  2. JS 加载html 在IE7 IE8下 可调试

    实际背景 就是都是HTML 公共头部底部  然后中间部分加载不同的HTML文件 有点跟模板引擎一样 jQuery 有个load函数 加载html文件的路径 获取html内容 到中间部分 正常下是不能用 ...

  3. 浅谈JavaScript中的defer,async

    引言 开始重读<<JavaScript高级程序设计>>一书,看到关于JavaScript中关于defer.async的部分.网上查询了点资料,觉得蛮好的.现在总结下. defe ...

  4. JQuery常用代码汇总

    获取<input />的value $("#id").val( ); 标签间的html $("#id").html('<tr><t ...

  5. 带有“非简单参数”的函数为什么不能包含 "use strict" 指令

    非简单参数就是 ES6 里新加的参数语法,包括:1.默认参数值.2.剩余参数.3.参数解构.本文接下来要讲的就是 ES7 为什么禁止在使用了非简单参数的函数里使用 "use strict&q ...

  6. [Math & Algorithm] 拉格朗日乘数法

    拉格朗日乘数法(Lagrange Multiplier Method)之前听数学老师授课的时候就是一知半解,现在越发感觉拉格朗日乘数法应用的广泛性,所以特意抽时间学习了麻省理工学院的在线数学课程.新学 ...

  7. GDI+ 笔记

    1.GDI+模板 #include<windows.h> #include<GdiPlus.h> #include <time.h> #include <ma ...

  8. javascript创建对象的一些方式

    通过创建一个Object实例 var person = new Object(); person.name = "zhouquan"; person.age = 21; perso ...

  9. PHP的大括号(花括号{})使用详解

    一.不管什么程序,function name(){}, for(){}, ….这太多了,不说也知道什么用了. 二.$str{4}在字符串的变量的后面跟上{}大括号和中括号[]一样都是把某个字符串变量当 ...

  10. class Solution(object): def fizzBuzz(self, n): a = [] i = 1 while(i <= n): if(i%15 == 0): a.append("FizzBuzz") elifleetcode day_01

    412. Fizz Buzz Write a program that outputs the string representation of numbers from 1 to n. But fo ...