-
C#教程之C#教程之MongoDB
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 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 } } |
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能