- A+
简单的先容一下,我使用MongoDB的场景。
我们如今的物联网情况下,有部门数据,采样频率为2000笔记录/分钟,如许下来一天24*60*2000=2880000约即是300万条数据,以后必然还会增长。之前数据库使用的是mssql,对付数据库的压力很年夜,同时又必要保证汗青查询的相应速率,这种环境下,在单表中数据量年夜,同时存在读写操作。不得已采纳MongoDB来存储数据。假如使用MongoDB,则至少必要三台机械,两台实现读写分别,一台作为仲裁(当然前提不容许也可以不消),每台机械的内存临时设置装备摆设在16G,公司小,没方法,听说,使用这个MongoDB必要机械内存起码92G,我没有验证过,然则吃内存是公认的,以是内存绝对要保证,就算保证了,也纷歧定完全就没故意外产生。我们上面的这些特殊的数据是容许少量的丧失的,这些只是做阐发使用的,几个月了,临时还没呈现数据丧失的环境,可能最新版本早就修复了吧,新手使用建议多看下官网上的阐明。下面直接奔入主题:
一、安装部署和设置装备摆设情况
1.安装部署mongo-server(V3.4)
参考 点击这里进入
这个时刻不要启动,接着设置装备摆设config文件
2.设置装备摆设Config文件
dbpath=C:/Program Files/MongoDB/Server/3.4/bin/data/db logpath=C:/Program Files/MongoDB/Server/3.4/bin/data/log/master.log pidfilepath=C:/Program Files/MongoDB/Server/3.4/bin/master.pid directoryperdb=true logappend=true replSet=testrs bind_ip=10.1.5.25 port=27016 oplogSize=10000 noauth = true storageEngine = wiredTiger wiredTigerCacheSizeGB = 2 syncdelay = 30 wiredTigerCollectionBlockCompressor = snappy
以上是具体的设置装备摆设参数,此中路径部门依据必要变动, 这里设置的oplogsize年夜小为10G,依据营业场景进行调整,另外auth权限为null,由于设置权限会增长服务开销,影响效力,最下面几行是内存引擎,可以节制副本集同步及内存限定,防止内存泄露。
3.启动mongo-server
4.添加副本集设置装备摆设
此时副本集集群设置装备摆设已经完成,然后在敕令行中输入:rs.status(),查看副本集状况,必要查看同步环境,可以输入敕令:db.serverStatus().
5.设置副本集可读写
Rs.slaveOk()
6..NET操作mongo
衔接设置,请参考小我封装Unitoon.Mongo代码所示。
7.机能对照
读写速率:Redis>Mongo>Mssqlserver
可容纳数据量:Mssqlserver~Mongo>Redis
存储数据类型:Mongo>Mssqlserver>Redis
Note:内存连续上升,内部没有内存收受接管机制,若限定内存 ,则可能呈现查询速率变慢,数据丧失等问题,建议优化查询效力,树立索引
Db.test.ensureIndex({"大众username"大众:1, "大众age"大众:-1})
强制开释内存敕令:db.runCommand({closeAllDatabases:1})
二、仓储设计
1.基类BaseEntity
namespace UnitoonIot.Mongo { /// <summary> /// 实体基类,便利天生ObjId /// </summary> [Serializable] [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)] //[ProtoInclude(10, typeof(NormalHistory))] public class BaseEntity { //[BsonRepresentation(BsonType.ObjectId)] public ObjectId Id { get; set; } /// <summary> /// 数据库名称 /// </summary> public string DbName { get; set; } /// <summary> /// 给工具初值 /// </summary> public BaseEntity() { // this.ObjId = ObjectId.GenerateNewId().ToString(); //this.Id = ObjectId.NewObjectId().ToString(); } } }
这里必要注意光阴格局,MongoDB默认光阴格局为国际光阴,以是在写入数据时和读取数据时,光阴格局要同等,此例中没有对光阴进行特殊处置,由传入的光阴格局肯定。
2.Repository承继接口IMongoRepository
namespace UnitoonIot.Mongo { public interface IMongoRepository<TEntity> where TEntity : class { } }
3.MongoRepository
using MongoDB.Driver; using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver.Linq; using System.Configuration; using System.IO; using UnitoonIot.AppSetting; namespace UnitoonIot.Mongo { public class MongoDb { private static string ConnectionStringHost ; private static string UserName ; private static string Password; private static IMongoDatabase _db = null; private static readonly object LockHelper = new object(); /// <summary> /// mongodb初始化 /// </summary> public static void Init() { ConnectionStringHost = "大众10.1.5.24:27016,10.1.5.24:27016,10.1.5.26:27017"大众; //AppSettings.GetConfigValue("大众MongoHost"大众);//"大众10.1.5.24:27016"大众; UserName = AppSettings.GetConfigValue("大众MongoUserName"大众); Password = AppSettings.GetConfigValue("大众MongoPwd"大众); } static MongoDb() { } public static IMongoDatabase GetDb(string dbName,string options=null) { if (_db != null) return _db; lock (LockHelper) { if (_db != null) return _db; var database = dbName; var userName = UserName; var password = Password; var authentication = string.Empty; var host = string.Empty; if (!string.IsNullOrWhiteSpace(userName)) { authentication = string.Concat(userName, ':', password, '@'); } if (!string.IsNullOrEmpty(options) && !options.StartsWith("大众?"大众)) { options = string.Concat('?', options); } host = string.IsNullOrEmpty(ConnectionStringHost) ? "大众localhost"大众 : ConnectionStringHost; database = database ?? "大众testdb"大众; //mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]][/[database][?options]] var conString = options!=null? $"大众mongodb://{authentication}{host}/{database}{options}"大众 : $"大众mongodb://{authentication}{host}/{database}"大众; var url = new MongoUrl(conString); var mcs = MongoClientSettings.FromUrl(url); mcs.MaxConnectionLifeTime = TimeSpan.FromMilliseconds(1000); var client = new MongoClient(mcs); _db = client.GetDatabase(url.DatabaseName); } return _db; } } /// <summary> /// MongoDb 数据库操作类 /// </summary> public class MongoRepository<T>: IMongoRepository<T> where T : BaseEntity { #region readonly field /// <summary> /// 表名 /// </summary> private readonly IMongoCollection<T> _collection = null; /// <summary> /// 数据库工具 /// </summary> private readonly IMongoDatabase _database; #endregion /// <summary> /// 构造函数 /// </summary> public MongoRepository() { this._database = MongoDb.GetDb(Activator.CreateInstance<T>().DbName, "大众readPreference =secondaryPreferred "大众);//primaryPreferred/secondaryPreferred/nearest _collection = _database.GetCollection<T>(typeof(T).Name); } #region 增长 /// <summary> /// 插入工具 /// </summary> /// <param name="大众t"大众>插入的工具</param> public virtual T Insert(T t) { // var flag = ObjectId.GenerateNewId(); // t.GetType().GetProperty("大众Id"大众).SetValue(t, flag); //t.Time = DateTime.Now; _collection.InsertOne(t); return t; } /// <summary> /// 批量插入 /// </summary> /// <param name="大众ts"大众>要插入的工具聚拢</param> public virtual IEnumerable<T> InsertBatch(IEnumerable<T> ts) { _collection.InsertMany(ts); return ts; } /// <summary> /// 插入工具 /// </summary> /// <param name="大众t"大众>插入的工具</param> public virtual void InsertAsync(T t) { //var flag = ObjectId.GenerateNewId(); // t.GetType().GetProperty("大众Id"大众).SetValue(t, flag); // t.Time = DateTime.Now; _collection.InsertOneAsync(t); } /// <summary> /// 批量插入 /// </summary> /// <param name="大众ts"大众>要插入的工具聚拢</param> public virtual void InsertBatchAsync(IEnumerable<T> ts) { _collection.InsertManyAsync(ts); } #endregion #region 删除 /// <summary> /// 删除 /// </summary> /// <returns></returns> public virtual long Delete(T t) { var filter = Builders<T>.Filter.Eq("大众Id"大众, t.Id); var result = _collection.DeleteOne(filter); return result.DeletedCount; } /// <summary> /// 删除 /// </summary> /// <returns></returns> public virtual void DeleteAsync(T t) { var filter = Builders<T>.Filter.Eq("大众Id"大众, t.Id); _collection.DeleteOneAsync(filter); } /// <summary> /// 按前提表达式删除 /// </summary> /// <param name="大众predicate"大众>前提表达式</param> /// <returns></returns> public virtual long Delete(Expression<Func<T, bool>> predicate) { var result = _collection.DeleteOne(predicate); return result.DeletedCount; } /// <summary> /// 按前提表达式删除 /// </summary> /// <param name="大众predicate"大众>前提表达式</param> /// <returns></returns> public virtual void DeleteAsync(Expression<Func<T, bool>> predicate) { _collection.DeleteOneAsync(predicate); } /// <summary> /// 按前提表达式批量删除 /// </summary> /// <param name="大众predicate"大众>前提表达式</param> /// <returns></returns> public virtual long DeleteBatch(Expression<Func<T, bool>> predicate) { var result = _collection.DeleteMany(predicate); return result.DeletedCount; } /// <summary> /// 按前提表达式批量删除 /// </summary> /// <param name="大众predicate"大众>前提表达式</param> /// <returns></returns> public virtual void DeleteBatchAsync(Expression<Func<T, bool>> predicate) { _collection.DeleteManyAsync(predicate); } /// <summary> /// 按检索前提删除 /// 建议用Builders<T>构建繁杂的查询前提 /// </summary> /// <param name="大众filter"大众>前提</param> /// <returns></returns> public virtual long Delete(FilterDefinition<T> filter) { var result = _collection.DeleteOne(filter); return result.DeletedCount; } /// <summary> /// 按检索前提删除 /// 建议用Builders<T>构建繁杂的查询前提 /// </summary> /// <param name="大众filter"大众>前提</param> /// <returns></returns> public virtual void DeleteAsync(FilterDefinition<T> filter) { _collection.DeleteOneAsync(filter); } #endregion #region 改动 /// <summary> /// 改动(Id不变) /// </summary> /// <returns></returns> public virtual long Update(T t) { var filterBuilder = Builders<T>.Filter; var filter = filterBuilder.Eq("大众Id"大众,t.Id); var update = _collection.WordStrOne(filter, t, new UpdateOptions() { IsUpsert = true }); return update.ModifiedCount; } /// <summary> /// 改动(Id不变) /// </summary> /// <returns></returns> public virtual void UpdateAsync(T t) { var filterBuilder = Builders<T>.Filter; var filter = filterBuilder.Eq("大众Id"大众, t.Id); _collection.WordStrOneAsync(filter, t, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 用新工具替换新文档 /// </summary> /// <param name="大众filter"大众>查询前提</param> /// <param name="大众t"大众>工具</param> /// <returns>改动影响文档数</returns> public virtual long Update(Expression<Func<T, bool>> filter, T t) { var update = _collection.WordStrOne(filter, t, new UpdateOptions() { IsUpsert = true }); return update.ModifiedCount; } /// <summary> /// 用新工具替换新文档 /// </summary> /// <param name="大众filter"大众>查询前提</param> /// <param name="大众t"大众>工具</param> /// <returns>改动影响文档数</returns> public virtual long Update(FilterDefinition<T> filter, T t) { var update = _collection.WordStrOne(filter, t, new UpdateOptions() { IsUpsert = true }); return update.ModifiedCount; } /// <summary> /// 用新工具替换新文档 /// </summary> /// <param name="大众filter"大众>查询前提</param> /// <param name="大众t"大众>工具</param> /// <returns>改动影响文档数</returns> public virtual void UpdateAsync(Expression<Func<T, bool>> filter, T t) { _collection.WordStrOneAsync(filter, t, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 用新工具替换新文档 /// </summary> /// <param name="大众filter"大众>查询前提</param> /// <param name="大众t"大众>工具</param> /// <returns>改动影响文档数</returns> public virtual void UpdateAsync(FilterDefinition<T> filter, T t) { _collection.WordStrOneAsync(filter, t, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 依据Id和前提文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众id"大众>工具Id</param> /// <returns>改动影响文档数</returns> public virtual long Update(string id, UpdateDefinition<T> update) { var filterBuilder = Builders<T>.Filter; var filter = filterBuilder.Eq("大众Id"大众, new ObjectId(id)); var result = _collection.UpdateOne(filter, update, new UpdateOptions() { IsUpsert = true }); return result.ModifiedCount; } /// <summary> /// 依据Id和前提文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众id"大众>工具Id</param> /// <returns>改动影响文档数</returns> public virtual void UpdateAsync(string id, UpdateDefinition<T> update) { var filterBuilder = Builders<T>.Filter; var filter = filterBuilder.Eq("大众Id"大众, new ObjectId(id)); _collection.UpdateOneAsync(filter, update, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 依据前提改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual void Update(UpdateDefinition<T> update,Expression<Func<T, bool>> filter) { _collection.UpdateOne(filter, update, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 依据前提改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual long Update(UpdateDefinition<T> update, FilterDefinition<T> filter) { var result = _collection.UpdateOne(filter, update, new UpdateOptions() { IsUpsert = true }); return result.ModifiedCount; } /// <summary> /// 依据前提改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual void UpdateAsync(UpdateDefinition<T> update, Expression<Func<T, bool>> filter) { _collection.UpdateOneAsync(filter, update, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 依据前提改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual void UpdateAsync(UpdateDefinition<T> update, FilterDefinition<T> filter) { _collection.UpdateOneAsync(filter, update, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 依据前提批量改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual long UpdateBatch(UpdateDefinition<T> update, Expression<Func<T, bool>> filter) { var result = _collection.UpdateMany(filter, update, new UpdateOptions() { IsUpsert = true }); return result.ModifiedCount; } /// <summary> /// 依据前提批量改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual long UpdateBatch(UpdateDefinition<T> update, FilterDefinition<T> filter) { var result = _collection.UpdateMany(filter, update, new UpdateOptions() { IsUpsert = true }); return result.ModifiedCount; } /// <summary> /// 依据前提批量改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual void UpdateBatchAsync(UpdateDefinition<T> update, Expression<Func<T, bool>> filter) { _collection.UpdateManyAsync(filter, update, new UpdateOptions() { IsUpsert = true }); } /// <summary> /// 依据前提批量改动文档 /// </summary> /// <param name="大众update"大众>改动前提-形如:Builders/<T/>.Update.Set(filed, value)</param> /// <param name="大众filter"大众>查询前提Builders/<T/>.Filter.Eq(filed, value)</param> /// <returns>改动影响文档数</returns> public virtual void UpdateBatchAsync(UpdateDefinition<T> update, FilterDefinition<T> filter) { _collection.UpdateManyAsync(filter, update, new UpdateOptions() { IsUpsert = true }); } #endregion #region 查询 #region GetCollection /// <summary> /// 获取操尴尬刁难象的IMongoCollection聚拢,强类型工具聚拢 /// </summary> /// <returns></returns> public virtual IMongoCollection<T> GetCollection() { return _database.GetCollection<T>(typeof(T).Name); } #endregion #region GetSingle /// <summary> /// 查询数据库,反省是否存在指定ID的工具 /// </summary> /// <param name="大众id"大众>工具的ID值</param> /// <returns>存在则返回指定的工具,不然返回Null</returns> public virtual T GetById(string id) { var filterBuilder = Builders<T>.Filter; var filter = filterBuilder.Eq("大众Id"大众, new ObjectId(id)); var data = _collection.Find(filter).FirstOrDefault(); return data; } /// <summary> /// 查询数据库,反省是否存在指定ID的工具 /// </summary> /// <param name="大众id"大众>工具的ID值</param> /// <returns>存在则返回指定的工具,不然返回Null</returns> public virtual async Task<T> GetAsyncById(string id) { var filterBuilder = Builders<T>.Filter; var filter = filterBuilder.Eq("大众Id"大众, new ObjectId(id)); var data = await _collection.FindAsync(filter); return await data.SingleOrDefaultAsync(); } /// <summary> /// 查询数据 /// </summary> /// <param name="大众filter"大众>过滤前提</param> /// <returns></returns> public virtual T Get(FilterDefinition<T> filter) { return _collection.Find(filter).FirstOrDefault(); } /// <summary> /// 查询数据 /// </summary> /// <param name="大众filter"大众>前提表达式</param> /// <returns></returns> public virtual T Get(Expression<Func<T,bool>> filter) { return _collection.Find(filter).FirstOrDefault(); } /// <summary> /// 查询数据 /// </summary> /// <param name="大众filter"大众>过滤前提</param> /// <returns></returns> public virtual async Task<T> GetAsync(FilterDefinition<T> filter) { var data = await _collection.FindAsync(filter); return await data.SingleOrDefaultAsync(); } /// <summary> /// 查询数据 /// </summary> /// <param name="大众filter"大众>前提表达式</param> /// <returns></returns> public virtual async Task<T> GetAsync(Expression<Func<T, bool>> filter) { var data = await _collection.FindAsync(filter); return await data.SingleOrDefaultAsync(); } #endregion #region GetMany /// <summary> /// 查询部门数据 /// </summary> /// <param name="大众filter"大众>过滤前提</param> /// <returns></returns> public virtual IEnumerable<T> GetMany(FilterDefinition<T> filter) { return _collection.Find(filter).ToEnumerable(); } /// <summary> /// 查询部门数据 /// </summary> /// <param name="大众filter"大众>前提表达式</param> /// <returns></returns> public virtual IEnumerable<T> GetMany(Expression<Func<T,bool>> filter) { //return _collection.AsQueryable().Where(filter).ToList(); //return _collection.AsQueryable().Where(filter); return _collection.Find(filter).ToEnumerable(); //.ToEnumerable(); } /// <summary> /// 查询部门数据 /// </summary> /// <param name="大众filter"大众>过滤前提</param> /// <returns></returns> public virtual async Task<IEnumerable<T>> GetManyAsync(FilterDefinition<T> filter) { var data = await _collection.FindAsync(filter); return await data.ToListAsync(); } /// <summary> /// 查询部门数据 /// </summary> /// <param name="大众filter"大众>过滤前提</param> /// <returns></returns> public virtual async Task<IEnumerable<T>> GetManyAsync(Expression<Func<T, bool>> filter) { var data = await _collection.FindAsync(filter); return await data.ToListAsync(); } #endregion #region GetAll /// <summary> /// 查询所有记载,繁杂查询直接用Linq处置(避免全表扫描) /// </summary> /// <returns>要查询的工具</returns> public virtual IEnumerable<T> GetAll() { var data = _collection.AsQueryable(); return data.ToEnumerable(); } /// <summary> /// 查询所有记载,繁杂查询直接用Linq处置(避免全表扫描) /// </summary> /// <returns>要查询的工具</returns> public virtual async Task<IEnumerable<T>> GetAllAsync() { var data = _collection.AsQueryable(); return await data.ToListAsync(); } /// <summary> /// 查询所有记载,繁杂查询直接用Linq处置(避免全表扫描) /// </summary> /// <returns>要查询的工具</returns> public virtual IQueryable<T> GetAllQueryable() { return _collection.AsQueryable(); } #endregion #region MapReduce /// <summary> /// MapReduce /// </summary> /// <returns>返回一个List列表数据</returns> public IEnumerable<T> GetMap(BsonJavaScript map,BsonJavaScript reduce) { return _collection.MapReduce<T>(map,reduce).ToList(); } #endregion #endregion } }
好了,就先容到这里。
您可能感兴致的文章:
关于Mongodb 认证鉴权你必要知道的一些事linux体系下MongoDB单节点安装教程vue+socket.io+express+mongodb 实现简略单纯多房间在线群聊示例node.js操作MongoDB的实例详解java操作mongoDB查询的实例详解MongoDB若何查询耗时记载的办法详解MongoDB 3.4 安装以 Windows 服务方式运行的具体步调详解MongoDB数据库根基操作及实例MongoDB TTL索引的实例详解