1.回顾:上篇学习了Mongodb的基本命令学习
2.这篇将学习使用Mongodb的C# Drivers实现增删改查
3.准备:
3.1 C# Drivers支持情况

面对现在的学习,我们还是使用 Version2.0版本,因为它支持全部的的.NET , 因为技术是越来越成熟的;
3.2 .NET 平台对 Mongodb版本的支持情况如下 :全部支持

Mongdb C#驱动 和 确定你已经配置和安装好你的Mongodb,若下载不下来的话,可以 点我下载!
4.使用
4.1 引用
(1) 打开vs2012及vs2012 版本以上 (.NET FrameWork4.5),新建项目,什么项目都行,能够测试就行,这里 使用控制台应用程序,
(2)引用 所有dll ;

4.2 连接mongdb
在连接之前,保证你 打开了 mongodb 服务,若不记得怎么打开,请访问 :点击学习打开服务
5. 新建MongoDb操作工具类
花了1个小时,写的工具类,测试测试吧!封装好了 mongdb连接 ,增删改查和创建集合
using MongoDB.Bson; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MongoDBDemo1 { public class Connection { //创建数据库连接 MongoServerSettings setting = new MongoServerSettings(); MongoServerAddress address =new MongoServerAddress("127.0.0.1", 27017); MongoServer server = null; string dbName=string.Empty; /// /// 得到连接 /// public Connection() { setting.Server = address; server = new MongoServer(setting); } /// /// 构造函数 获得 dbname /// ///数据库名称 public Connection(string dbname) { this.dbName = dbname; setting.Server = address; server = new MongoServer(setting); } /// /// 外部调用得到数据库 /// //////
public MongoDatabase getMongoDataBase(string dbName) { return server.GetDatabase(dbName); } /// /// 重载 得到数据库 /// ///
public MongoDatabase getMongoDataBase() { return server.GetDatabase(dbName); } /// /// 创建集合 /// ///集合名字 ///
public bool createCollection(string colName) { MongoDatabase db = this.getMongoDataBase(); CommandResult result= db.CreateCollection(colName); if (result.Ok) { return true; } else { return false; } } /// /// 外部调用 /// ///集合名称 ///数据库对象 ///
public MongoCollection
getMongoColl(string colName,MongoDatabase db){ return db.GetCollection(colName); } ///
/// 内部使用 /// ///
集合名称 ///
public MongoCollection
getMongoColl(string colName){ return this.getMongoDataBase().GetCollection(colName); } ///
/// 查询操作 /// ///
集合名词 ///
条件 ///
public MongoCursor
select(string colName,IMongoQuery query){ MongoCollection
mcoll=getMongoColl(colName); try { if (query == null) { return mcoll.FindAll(); } else { return mcoll.Find(query); } } finally { this.mongoClose(); } } ///
/// 增加操作 /// ///
集合名称 ///
单条数据 ///
public bool insert(string colName,BsonDocument bson) { MongoCollection
mcoll = getMongoColl(colName); try { mcoll.Insert(bson); return true; } catch { return false; } finally { this.mongoClose(); } } ///
/// 增加多条 /// ///
集合名称 ///
数据集合 ///
public bool insert(string colName, List
bsonlist) { MongoCollection
mcoll = getMongoColl(colName); try { foreach (BsonDocument bson in bsonlist) { mcoll.Insert(bson); } return true; } catch { return false; } finally { this.mongoClose(); } } ///
/// 修改操作 /// ///
集合名称 ///
条件 ///
修改的内容 ///
public bool update(string colName, IMongoQuery query,IMongoUpdate upd) { MongoCollection
mcoll = getMongoColl(colName); try { mcoll.Update(query, upd); return true; } catch { return false; } finally { this.mongoClose(); } } ///
/// 删除操作 /// ///
集合名称 ///
条件 ///
是否为0 ,为0 则删除 满足条件的全部信息 ///
false/true
public bool delete(string cloName, IMongoQuery query,int flag) { MongoCollection
mcol = this.getMongoColl(cloName); try { if (flag == 0) { mcol.Remove(query, RemoveFlags.None); } else { mcol.Remove(query, RemoveFlags.Single); } return true; } catch { return false; } finally { this.mongoClose(); } } ///
/// 关闭连接 /// public void mongoClose(){ server.Discon