mongodb

一,准备材料

 官网下载MongoDB安装包:http://www.mongodb.org/downloads

 百度云盘下 https://pan.baidu.com/s/1pLTnkld  密码 dvsw

   建议在官网下载避免XCode

   安装完成如图

     

 logs 就是一个空文件夹 可以自行创建

 然后创建数据库目录 D:\data\db

二,启动服务

打开cmd 切换磁盘目录

执行windows服务安装指令

mongod.exe --logpath D:\mongodb\logs\MongoDB.log --logappend --dbpath D:\mongodb\data\db --directoryperdb --serviceName MongoDB --install

出现问题一般是路径有问题。
1)请注意你所有的路径没有错,包括mongod所在路径,日志所在路径等;
2)不要加入多余的“”,如“D:MongoDBData”这个,千万不要写成“D:MongoDBData”
安装完成服务
net start mongodb  启动服务
net stop mongodb   停止服务
sc delete mongodb   删除服务(这条命令适合大多数服务,只需要改后面的服务名称)

删除服务指令

sc delete MongoDB

 三,.NET连接

    /// <summary>
    /// mongodb工厂
    /// </summary>
    public class MongoFactory
    {
        /// <summary>
        /// 获取mongodb数据连接
        /// </summary>
        /// <param name="dataBaseName">数据库名</param>
        /// <returns>数据连接基础类</returns>
        public static MongoDatabase GetMongoDatabase(string dataBaseName)
        {
            MongoServerSettings mongoServerSettings = new MongoServerSettings
            {
                Server = new MongoServerAddress("127.0.0.1", 27017),
                MaxConnectionPoolSize = 20,
                MinConnectionPoolSize = 5,
            };
            MongoServer mongoServer = new MongoServer(mongoServerSettings);
            return mongoServer.GetDatabase(dataBaseName);
        }

        /// <summary>
        ///  获取字典数据库
        /// </summary>
        /// <returns>数据连接基础类</returns>
        public static MongoDatabase GetMongoDatabaseByDic()
        {
            const string dbName = "Dic"; // 可以写成配置文件
            MongoDatabase mongoDatabase = GetMongoDatabase(dbName);
            return mongoDatabase;
        }
    }
    /// <summary>
    /// 测试数据
    /// </summary>
    public class TestData
    {
        /// <summary>
        /// 初始化字典数据
        /// </summary>
        public static void InitDicData()
        {
            var data = Dic.Default;
            string code = "01234";
            DateTime tempTime = GetLastUpdateTime(code);
            if (tempTime != default(DateTime))
            {
                UpdateData(data);
            }
            else
            {
                AddData(data);
            }
            // 更新更新时间
            SetUpdateTime(code, DateTime.Now);
        }

        /// <summary>
        /// 根据key查看数据
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>数据</returns>
        public static List<Dic> ViewTheData(string key)
        {
            // 初始化数据库
            MongoDatabase mongoDatabase = MongoFactory.GetMongoDatabaseByDic();
            // 初始化表
            MongoCollection mongoCollection = mongoDatabase.GetCollection("dicTable");
            List<IMongoQuery> queryList = new List<IMongoQuery>
            {
                Query.EQ("Key", key),
            };
            IMongoQuery query = Query.And(queryList);
            List<Dic> dics = mongoCollection.FindAs<Dic>(query).ToList();
            return dics;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="data">数据</param>
        public static void AddData(List<Dic> data)
        {
            // 初始化数据库
            MongoDatabase mongoDatabase = MongoFactory.GetMongoDatabaseByDic();
            // 初始化表
            MongoCollection mongoCollection = mongoDatabase.GetCollection("dicTable");
            // 插入数据
            foreach (var item in data)
            {
                mongoCollection.Insert<Dic>(item);
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="data">数据</param>
        public static void UpdateData(List<Dic> data)
        {
            // 初始化数据库
            MongoDatabase mongoDatabase = MongoFactory.GetMongoDatabaseByDic();
            // 初始化表
            MongoCollection mongoCollection = mongoDatabase.GetCollection("dicTable");
            foreach (var item in data)
            {
                var query = GetQueryCondition(item);
                mongoCollection.Remove(query);
                mongoCollection.Insert(item);
            }
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <param name="item">dic</param>
        /// <returns>查询条件</returns>
        public static IMongoQuery GetQueryCondition(Dic item)
        {
            // 时间列子
            // queryList.Add(Query.EQ("列名", new BsonDateTime(DateTime.Now)));
            List<IMongoQuery> queryList = new List<IMongoQuery>
            {
                Query.EQ("Key", item.Key),
                Query.EQ("Value", item.Value)
            };
            IMongoQuery query = Query.And(queryList);
            return query;
        }

        /// <summary>
        /// 设置更新时间
        /// </summary>
        /// <param name="code">业务代码</param>
        /// <param name="dateTime">更新时间</param>
        public static void SetUpdateTime(string code, DateTime dateTime)
        {

            MongoDatabase mongoDatabase = MongoFactory.GetMongoDatabaseByDic();
            MongoCollection mongoCollection = mongoDatabase.GetCollection("UpdateTime");
            List<IMongoQuery> queryList = new List<IMongoQuery>
            {
                Query.EQ("_id", code)
            };
            var query = Query.And(queryList);
            mongoCollection.Remove(query);
            UpdateTime updateTime = new UpdateTime
            {
                Key = code,
                Time = dateTime
            };
            mongoCollection.Insert(updateTime);
        }

        /// <summary>
        /// 获取最后更新时间
        /// </summary>
        /// <param name="code">业务代码</param>
        /// <returns>时间</returns>
        public static DateTime GetLastUpdateTime(string code)
        {
            MongoDatabase mongoDatabase = MongoFactory.GetMongoDatabaseByDic();
            MongoCollection mongoCollection = mongoDatabase.GetCollection("UpdateTime");
            List<IMongoQuery> queryList = new List<IMongoQuery>
            {
                Query.EQ("Key", code)
            };
            var query = Query.And(queryList);
            List<UpdateTime> time = mongoCollection.FindAs<UpdateTime>(query).ToList();
            if (time.Count == 0)
            {
                return default(DateTime);
            }
            else
            {
                return time[0].Time;
            }
        }
    }

    /// <summary>
    /// 字典实体
    /// </summary>
    public class Dic
    {
        /// <summary>
        ///  BsonType.ObjectId 这个对应了 MongoDB.Bson.ObjectId
        /// </summary>
        public ObjectId _id { get; set; }

        /// <summary>
        /// key
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// value
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// 默认数据用于测试
        /// </summary>
        public static List<Dic> Default => new List<Dic>
        {
            new Dic
            {
                Key = "k1",
                Value = "v1"
            },
            new Dic
            {
                Key = "k2",
                Value = "v2"
            },
            new Dic
            {
                Key = "k3",
                Value = "v3"
            }
        };
    }

    /// <summary>
    /// 更新时间
    /// </summary>
    public class UpdateTime
    {
        /// <summary>
        ///  BsonType.ObjectId 这个对应了 MongoDB.Bson.ObjectId
        /// </summary>
        public ObjectId _id { get; set; }

        /// <summary>
        /// 业务代码
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime Time { get; set; }
    }

测试 调用

    /// <summary>
    /// Program
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Main
        /// </summary>
        /// <param name="args">args</param>
        public static void Main(string[] args)
        {
            TestData.InitDicData();
            var data = TestData.ViewTheData("k1");
        }
    }

 

posted @ 2017-02-28 11:09  刘小吉  阅读(245)  评论(0编辑  收藏  举报