示例#1
0
        /// <summary>
        /// 创建索引
        /// </summary>
        public void CreateIndex()
        {
            LogService.Write("创建索引...");
            try
            {
                //创建索引配置库索引
                MongoDatabase dbWcf = MongoDBConn.GetDatabase("WcfLogServer");//数据库名字
                MongoCollection <BsonDocument> tableInfoSetting = dbWcf["InfoSetting"];
                var indexesWcf = tableInfoSetting.GetIndexes().Select(a => a["name"].ToString()).ToList();
                if (!indexesWcf.Any(index => index.Contains("CollectionName")))
                {
                    tableInfoSetting.CreateIndex(IndexKeys.Descending("CollectionName"), IndexOptions.SetBackground(true).SetUnique(false));
                    LogService.Write("创建库:WcfLogServer 表:InfoSetting 列:CollectionName的索引");
                }

                //创建数据库索引
                string             dbName  = "WcfLog" + GetCurrentDate();
                string[]           orderby = { "CreateTime" };
                List <InfoSetting> list    = MongoDBUtil.GetAll <InfoSetting>("WcfLogServer", "InfoSetting", new BsonDocument(), orderby, new BsonDocument());
                if (list != null && list.Count > 0)
                {
                    MongoDatabase db = MongoDBConn.GetDatabase(dbName);//数据库名字
                    foreach (InfoSetting info in list)
                    {
                        if (!string.IsNullOrEmpty(info.IndexName))
                        {
                            MongoCollection <BsonDocument> table = db[info.CollectionName];
                            var indexes = table.GetIndexes().Select(a => a["name"].ToString()).ToList();

                            List <string> listIndexes = info.IndexName.Split(',').ToList();
                            if (listIndexes != null && listIndexes.Count > 0)
                            {
                                foreach (string item in listIndexes)
                                {
                                    if (!indexes.Any(index => index.Contains(item)))
                                    {
                                        table.CreateIndex(IndexKeys.Ascending(item), IndexOptions.SetBackground(true).SetUnique(false));
                                        LogService.Write("创建库:" + dbName + " 表:" + info.CollectionName + " 列:" + item + "的索引");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogService.Write(ex);
            }
        }
示例#2
0
        public void GetTestData()
        {
            string dbName = "WcfLog" + GetCurrentDate();

            string[] orderby = { "Time" };
            List <ClientExceptionInfo> list = MongoDBUtil.GetPaged <ClientExceptionInfo>(dbName, "ClientExceptionInfo", new BsonDocument(), orderby, new BsonDocument(), 1, 100);

            if (list != null && list.Count > 0)
            {
                Console.WriteLine("count:" + list.Count);
                foreach (ClientExceptionInfo ce in list)
                {
                    Console.WriteLine("id:{0} ip:{1} time:{2}", ce.ID, ce.MachineIP, ce.Time);
                }
            }
        }