VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之MongoDB

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

1.什么是MongoDB?

官网介绍:MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need

维基百科:MongoDB是一种面向文档的数据库管理系统,由C++撰写而成

百度百科:MongoDB 是一个基于分布式文存储的数据库

2.为什么我要使用MongoDB

最近写一个项目,很多业务需要在调度中多线程处理。使用的是LINQ+EF,数据库是msSQL,中间业务不算复杂,关系七八张表,中间有IO操作,GIT操作,CMD命令操作等  ..速度实在是没有办法忍受.

大概几千个文件提交需要执行30分钟。。。。  

后来了解到有MongoDB这种数据库,性能高,灵活,扩展性高等等,再根据我们代码和业务的实际情况,就用准备测试一下实际情况

然后根据业务和一些性能考虑将调度代码分成三部分,

再次测速的几个文件提交只花了十几秒钟的时间。

MongoDB功不可没。

 3.下载

官网下载就可以了 https://www.mongodb.com/

4.安装

一直点下一步就好了

5.使用

贴一点代码,非真实项目中代码,还有需要修改的地方,比如反射没使用委托等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
 
namespace Model
{
    public static class MongoDBHelperNow<T>
    {
 
        /// <summary>
        /// 数据库连接
        /// </summary>
        static MongoClient client = new MongoClient("mongodb://127.0.0.1:27017");//ConstDefine.MongoDBConnectionString
        /// <summary>
        /// 数据库名
        /// </summary>
        private static readonly IMongoDatabase _gitDatabase = client.GetDatabase("Blog");//"Git"ConstDefine.MongoDBGitTableName
 
        public static IMongoDatabase GitDb { get return _gitDatabase; } }
 
        public static IMongoCollection<T> GitTable(string keyname) => _gitDatabase.GetCollection<T>(keyname);
 
        private static string GetTableName() => typeof(T).ToString().Split('_')[1];
 
        #region 增  
        public static void InsertOne(T entity) => GitTable(GetTableName()).InsertOne(entity);
        public static void InsertOneAsync(T entity) => GitTable(GetTableName()).InsertOneAsync(entity);
        public static void InsertList(List<T> entity) => GitTable(GetTableName()).InsertMany(entity);
        public static void InsertListAsync(List<T> entity) => GitTable(GetTableName()).InsertManyAsync(entity);
        #endregion 
        #region 删
        public static void DeleteOne(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).FindOneAndDelete(whereLambda);
        public static void DeleteOneAsync(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).FindOneAndDeleteAsync(whereLambda);
        public static void DeleteList(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).DeleteMany(whereLambda);
        public static void DeleteListAsync(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).DeleteManyAsync(whereLambda);
        #endregion
        #region 查
        public static T FindOne(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).Find(whereLambda).FirstOrDefault();
        public static List<T> FindList(Expression<Func<T, bool>> whereLambda) => GitTable(GetTableName()).Find(whereLambda).ToList();
        #endregion
        #region 改
        public static T ReplaceOne(Expression<Func<T, bool>> whereLambda,T entity) => GitTable(GetTableName()).FindOneAndReplace(whereLambda, entity); 
        public static Task<T> ReplaceOneAsync(Expression<Func<T, bool>> whereLambda, T entity) => GitTable(GetTableName()).FindOneAndReplaceAsync(whereLambda, entity); 
        #endregion
    }
}   
相关教程