zoukankan      html  css  js  c++  java
  • 我的Mongodb旅程

    一,关于安装

      Mongodb的安装百度出来一大把,在此就不赘叙了,安装过程按个人经验及熟练程序,大概1-3天能够完成整个开发环境的搭建,只说明安装过程中需要注意的几点:

      1,找资料的时候注意看发表的时间,然后看官网的3.0的发布时间,因为3.0跟之前的版本区别很大,如果不小心找到3.0之前的一些资料,很可能会被坑,哪怕你安装完成了,后面实际操作的时候总会发现有问题,严重的话可能导致重新安装并重写程序;

      2,找官网下载最新的安装程序和对应的开发语言的驱动,我的是。net,驱动也是一样找最新的,不然也是有坑的;

      3,可视化工具,找了很多,感觉最好用的还是mongobooster。

    二,关于开发

      主要用到两个类:MongoModel和MongoHelper

        public abstract class MongoModel
        {
            /// <summary>
            /// orderId
            /// </summary>
            public long oId { get; set; }
            public ObjectId id { get; set; }
        }

    oId 是用来分页的,系统自带的skip分页性能太差,下面的MongoHelper有点长。

    public class MongoHelper<T> where T : Model.MongoModel
        {
            public string dbName;
            public string collectionName;
    
            public IMongoCollection<T> collection;
    
            private IMongoCollection<IDS> idColl;
    
            public MongoHelper()
            {
                if (string.IsNullOrWhiteSpace(ConfModel.mongodb_dbAddr))
                    InIClass.InitilizeMongoConfig();
                collectionName = typeof(T).Name;
                dbName = ConfModel.mongodb_dbName;
                SetCollection();
    
            }
    
            /// <summary>
            /// 设置你的collection
            /// </summary>
            public void SetCollection()
            {
                string login = ConfModel.mongodb_uName;
                SecureString pass = Extensions.GetSecureString(ConfModel.mongodb_uPwd);
                string authDB = ConfModel.mongodb_dbName;
                var settings = new MongoClientSettings
                {
                    Credentials = new[] { MongoCredential.CreateMongoCRCredential(authDB, login, pass) },
                    Server = new MongoServerAddress(ConfModel.mongodb_dbAddr, int.Parse(ConfModel.mongodb_port))
                };
                MongoClient mongoClient = new MongoClient(settings);
    
                var database = mongoClient.GetDatabase(dbName);
                collection = database.GetCollection<T>(collectionName);
                idColl = database.GetCollection<IDS>(typeof(IDS).Name);
            }
            /// <summary>
            /// 你用linq的时候会用到
            /// </summary>
            public void getCollection()
            {
                MongoClient client = new MongoClient(ConfModel.mongodb_conn);
    
                var database = client.GetDatabase(dbName);
                collection = database.GetCollection<T>(collectionName);
            }
            /// <summary>
            /// 查找
            /// </summary>
            /// <param name="query"></param>
            /// <returns></returns>
            public T Find(FilterDefinition<T> query)
            {
                return this.collection.Find(query).FirstOrDefault();
            }
    
            public T FindOne(FilterDefinition<T> query, Dictionary<string, int> sortfields)
            {
                SortDefinition<T> orderBy = getSortDefinition(sortfields);
                return this.collection.Find(query).Sort(orderBy).FirstOrDefault();
            }
            /**
             * 条件查询用linq
             * http://mongodb.github.io/mongo-csharp-driver/1.11/linq/
             * Query.All("name", "a", "b");//通过多个元素来匹配数组
             * Query.And(* Query.EQ("name", "a"),Query.EQ("title", "t"));//同时满足多个条件
             * Query.EQ("name", "a");//等于
             * Query.Exists("type", true);//判断键值是否存在
             * Query.GT("value", 2);//大于>
             * Query.GTE("value", 3);//大于等于>=
             * Query.In("name", "a", "b");//包括指定的所有值,可以指定不同类型的条件和值
             * Query.LT("value", 9);//小于
             * Query.LTE("value", 8);//小于等于
             * Query.Mod("value", 3, 1);//将查询值除以第一个给定值,若余数等于第二个给定值则返回该结果
             * Query.NE("name", "c");//不等于
             * Query.Nor(Array);//不包括数组中的值
             * Query.Not("name");//元素条件语句
             * Query.NotIn("name", "a", 2);//返回与数组中所有条件都不匹配的文档
             * Query.Or(* Query.EQ("name", "a"), * Query.EQ("title", "t"));//满足其中一个条件
             * Query.Size("name", 2);//给定键的长度
             * Query.Type("_id", BsonType.ObjectId );//给定键的类型
             * Query.Where(BsonJavaScript);//执行JavaScript
             * Query.Matches("Title",str);//模糊查询 相当于sql中like  -- str可包含正则表达式
             **/
            public List<T> FindList(FilterDefinition<T> query)
            {
                //this.collection.Find(query).SetSortOrder(new SortByDocument("name1", 1)).SetLimit(2).ToList();
                return this.collection.Find(query).ToList();
            }
            /// <summary>
            /// 查找列表
            /// </summary>
            /// <param name="query">详情右键转到定义查看备注</param>
            /// <param name="orderBy">字段名称</param>
            /// <param name="desc">1=正序;-1=倒序</param>
            /// <returns></returns>
            public List<T> FindList(FilterDefinition<T> query, string orderBy, int desc)
            {
                Dictionary<string, int> sortfields = new Dictionary<string, int>() { { orderBy, desc } };
    
                SortDefinition<T> orders = getSortDefinition(sortfields);
                return this.collection.Find(query).Sort(orders).ToList();
            }
            public List<T> FindList(FilterDefinition<T> query, string orderBy, int desc, int pageIndex, int pageSize)
            {
                if (query == null) return null;
                Dictionary<string, int> sortfields = new Dictionary<string, int>() { { orderBy, desc } };
                SortDefinition<T> orders = getSortDefinition(sortfields);
                return this.collection.Find(query).Sort(orders).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList();
            }
            public List<T> FindList(FilterDefinition<T> query, string orderBy, int desc, int pageIndex, int pageSize, ref int total)
            {
                if (query == null) return null;
                total = (int)this.collection.Find(query).Count();
                if (string.IsNullOrEmpty(orderBy))
                    return this.collection.Find(query).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList();
                Dictionary<string, int> sortfields = new Dictionary<string, int>() { { orderBy, desc } };
                SortDefinition<T> orders = getSortDefinition(sortfields);
                return this.collection.Find(query).Sort(orders).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList();
            }
            private IDS GetIDS(int incCount)
            {
                FilterDefinition<IDS> filter = Builders<IDS>.Filter.Eq("Key", typeof(T).Name);
                IDS id = this.idColl.Find(filter).FirstOrDefault();
                if (id == null)
                {
                    id = new IDS() { Key = typeof(T).Name, Value = incCount };
                    idColl.InsertOne(id);
                    id.Value = 0;
                    return id;
                }
                else
                {
                    UpdateDefinition<IDS> updateDef = Builders<IDS>.Update.Inc("Value", incCount);
                    var result = idColl.FindOneAndUpdate<IDS>(filter, updateDef);
                    return result;
                }
            }
    
            /// <summary>
            /// 添加
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            public void Insert(T model)
            {
                IDS idModel = GetIDS(1);
                model.oId = idModel.Value + 1;
                this.collection.InsertOne(model);
            }
    
            /// <summary>
            /// 批量插入
            /// </summary>
            public void InsertBatchLogin(List<T> model)
            {
                InsertManyOptions options = new InsertManyOptions
                {
                    BypassDocumentValidation = true
                };
                try
                {
                    IDS idModel = GetIDS(model.Count);
                    for (int i = 0; i < model.Count; i++)
                    {
                        model[i].oId = idModel.Value + i;
                    }
                    this.collection.InsertMany(model, options);
                }
                catch (Exception ex)
                {
                    //批量插入异常,记录日志
                    Console.WriteLine("批量插入异常:{0}", ex.ToString());
                    throw ex;
                }
            }
            /// <summary>
            /// 修改
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            public long Update(T model)
            {
                Dictionary<string, object> updatedic = new Dictionary<string, object>();
                FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", model.id);
    
                ReplaceOneResult res = this.collection.ReplaceOne(filter, model);
                return res.ModifiedCount;
            }
            public void UpdateMany(FilterDefinition<T> where, Dictionary<string, object> set)
            {
                this.collection.UpdateManyAsync(where, getUpdateDefinition(set));
            }
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            public long Delete(T model)
            {
                FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", model.id);
                DeleteResult res = this.collection.DeleteMany(filter);
                return res.DeletedCount;
            }
            public long DeleteMany(FilterDefinition<T> where)
            {
                DeleteResult res = this.collection.DeleteMany(where);
                return res.DeletedCount;
            }
            public static SortDefinition<T> getSortDefinition(Dictionary<string, int> sortfields)
            {
                SortDefinition<T> sd = null;
                foreach (var item in sortfields)
                {
                    if (sd == null)
                    {
                        if (item.Value == 1)
                        {
                            sd = Builders<T>.Sort.Ascending(item.Key);
                        }
                        else
                        {
                            sd = Builders<T>.Sort.Descending(item.Key);
                        }
                    }
                    else
                    {
                        if (item.Value == 1)
                        {
                            sd.Ascending(item.Key);
                        }
                        else
                        {
                            sd.Descending(item.Key);
                        }
                    }
                }
                return sd;
            }
            public static UpdateDefinition<T> getUpdateDefinition(Dictionary<string, object> updatedic)
            {
                var fieldList = new List<UpdateDefinition<T>>();
                UpdateDefinition<T> ud = null;
                foreach (var item in updatedic)
                {
                    if (item.Key != "id")
                    {
    
                        if (ud == null)
                        {
                            ud = Builders<T>.Update.Set(item.Key, item.Value);
                        }
                        else
                        {
                            ud.AddToSet(item.Key, item.Value);
                        }
                    }
                }
                return ud;
            }
    
        }
    View Code

    然后就是调用了,类似:


    public class DEMOMongo
    {
    public static MongoHelper<DEMO> Mongoclient = new MongoHelper<DEMO>();

    public static void test ()

    {

    //TODO: u code

    }

    }

    怎么样,简单吧,而且性能杠杠的,增删查改啥的不超过十万条都是几乎瞬间完成的,几乎完美。

    但是它有个致命的缺陷,每次查询的结果都会载入内存,直至把内存占光为止,然后性能就直线下降了!

    不过对于想尝试nosql的同学还是值得一试的,而且对于数据量不是很大的一些内部系统用这个还是很不错的选择,比较性能牛逼啊!

    初次尝试写博客,表达不是很好,见笑!

  • 相关阅读:
    leetcode Power of Two
    Lowest Common Ancestor of a Binary Search Tree
    Lowest Common Ancestor of a Binary Tree
    leetcode Delete Node in a Linked List
    leetcode Search a 2D Matrix II
    leetcdoe Valid Anagram
    leetcode Add Digits
    leetcode Single Number III
    leetcode Ugly Number
    leetcode Ugly Number II
  • 原文地址:https://www.cnblogs.com/Ajuice/p/5787697.html
Copyright © 2011-2022 走看看