MongoDB-C#驱动帮助
查增改删
链接字符串 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#驱动帮助的更多相关文章
- (转载)MongoDB C#驱动中Query几个方法
MongoDB C#驱动中Query几个方法 Query.All("name", "a", "b");//通过多个元素来匹配数组 Query ...
- 使用VS2010编译MongoDB C++驱动详解
最近为了解决IM消息记录的高速度写入.多文档类型支持的需求,决定使用MongoDB来解决. 考虑到MongoDB对VS版本要求较高,与我现有的VS版本不兼容,在leveldb.ssdb.redis.h ...
- MongoDB C#驱动:
MongoDB C#驱动: http://xiaosheng.me/2016/09/15/article24 http://www.cnblogs.com/wuhuacong/p/5098348.ht ...
- MongoDB C#驱动
烟波钓徒 MongoDB C#驱动 http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial 笔记 首先下载驱动.驱动有两个文件 Mongo ...
- [转载]MongoDB C# 驱动教程
本教程基于C#驱动 v1.6.x . Api 文档见此处: http://api.mongodb.org/csharp/current/. 简介 本教程介绍由10gen支持的,用于MongoDB的C# ...
- MongoDB操作(1)—MongoDB java驱动核心层次结构及操作流程
MongoDB之java驱动学习 预备: 本地运行MongoDB采用默认端口20717: 安装MongoDB驱动: 以下关键步骤. 核心层次结构或步骤: 创建连接池:MongoClient实例. 对于 ...
- MongoDB C# 驱动教程
C# 驱动版本 v1.6.x 本教程基于C#驱动 v1.6.x . Api 文档见此处: http://api.mongodb.org/csharp/current/. 简介 本教程介绍由10gen支 ...
- [转]MongoDB c++驱动安装与使用
安装 获取源码:git clone https://github.com/mongodb/mongo-cxx-driver.git,解压 安装编译工具scons:yum install -y scon ...
- [原创]MongoDB C++ 驱动部分问题解决方案(MongoDB C++ Driver)
本文为我长时间开发以及修改MongoDB C++ Driver时的一些问题和解决方案.目前本文所介绍的相关引擎也已经发布闭源版本,请自行下载 库版本以及相关位置:http://code.google. ...
- MongoDb C# 驱动操作示例
c#操作mongo数据库 驱动采用http://www.oschina.net/p/mongo-csharp-driver C#驱动的基本数据库连接,增删改查操作 //定义对象 public clas ...
随机推荐
- 【06-23】js动画学习笔记01
<html> <head> <style> * { margin:0; padding:0; } #div1{ width:200px; height:200px; ...
- PHP图片上传类
前言 在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也很有必要. 图片上传的流程图 一.控制器调用 public function upload_file() { if (IS_P ...
- Ajax 应用六个需要注意的事项
接触Ajax,那时候的Ajax支持还不是很好,都要涉及底层,没有现成的框架给你调用.现在把常见的问题列举如下.1.编码问题注意AJAX要取的文件是UTF-8编码的.GB2312编码传回BROWSE后中 ...
- (五)SQL Server分区自动化案例
需求定义 统计表可能达到每天1000万数据.只查询当天的数据用于统计,可归档三月前的数据.得出分区方案如下: 每天生成一个分区 归档三个月前的分区 基本架构 固定生成12个辅助数据库文件,将每年当月的 ...
- Daily Scrum Meeting ——ThirdDay
一.Daily Scrum Meeting照片 二.Burndown Chart 三.项目进展 1.完成了github上的文档整理 Transcend/ActivityHelper 2.主界面侧滑框的 ...
- ecshop二次开发常用代码
通过index控制循环次数.在循环中将index作为if的判断条件,对循环进行控制.这里index是从0开始的.如果index<2,会进行两次循环. <!--{foreach from=$ ...
- iOS 引入framework的常见问题和原理
今天在引入第三方framework时,我按照以前的方法,把framework加入到了下图的地方: 默认是required的,之后程序就crash了,报错dyld: Library not loaded ...
- 使用Angular2理由
1. 组件化 组件化编程是web 发展的一个趋势,Angular2提供高效简单的组件开发方式,使程序开发更加关注业务逻辑的实现,而不用关心如何加载组件和模块,如何引用及依赖注入的实现等. 如下代码所示 ...
- java获得汉语首字母
package org.scbit.lsbi.scp.utils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefor ...
- 关于kali2.0rolling中metasploit升级后无法启动问题的解决总结
最近在学习metasploit的使用,文中提到可以使用msfupdate命令来对metasploit的payload.exploit等进行升级,我就试了一下,没想到升级过程并不麻烦,但升级后却出现了无 ...