MongoDB
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 } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律