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 ...
随机推荐
- getCanonicalName和getSimpleName getName的区别与应用
接口: package com.test; public interface Fruit { } 一个实现类: package com.test; public class Apple impleme ...
- eclipse中 linked resource的使用
一.关于linked resource eclipse 中的linkded resources 是指存放在项目所在位置以外某个地方的文件或者文件夹:这些特定的资源必须有一个项目作为他们的父资源.l ...
- 在Activity之间传递参数(三)——serializable和parcelable的区别
传递值对象: 一.serializable实现:简单易用 serializable的迷人之处在于你只需要对某个类以及它的属性实现Serializable 接口即可.Serializable 接口是一种 ...
- [Data Structure & Algorithm] 八大排序算法
排序有内部排序和外部排序之分,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.我们这里说的八大排序算法均为内部排序. 下图为排序 ...
- iframe的自适应高度
<iframe src="index.html" id="iframepage" name="iframepage" frameBor ...
- PHP header函数使用大全
PHP header函数大全 header('Content-Type: text/html; charset=utf-8'); header('Location: http://52php.cnbl ...
- 第2月第25天 BlocksKit
1.blockskit https://github.com/zwaldowski/BlocksKit bk_showAlertViewWithTitle 2.toast +(void)showToa ...
- java笔记--关于线程同步(7种同步方式)
关于线程同步(7种方式) --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3897440.html"谢谢-- 为何要使用同步? ...
- struts2表单批量提交
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%> <% ...
- sdcms留言提交
引入这两个js <script src="{webroot}lib/validator/jquery.validator.js"></script>< ...