欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB

程序员文章站 2024-01-03 23:38:40
1.什么是MongoDB? 官网介绍:MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need * ......

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.下载

官网下载就可以了 

4.安装

一直点下一步就好了

5.使用

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

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
	}
}   

上一篇:

下一篇: