Exemplo n.º 1
0
        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="collection">集合名</param>
        /// <param name="index">索引键</param>
        /// <param name="key"></param>
        /// <param name="asc"></param>
        /// <returns></returns>
        public string CreateIndex <T>(string collection, string index, Expression <Func <T, object> > key, bool asc = true)
        {
            IMongoIndexManager <T> mgr = Database.GetCollection <T>(collection).Indexes;
            var list = mgr.List();

            while (list.MoveNext())
            {
                if (!list.Current.Any(doc => doc["name"].AsString.StartsWith(index)))
                {
                    return(mgr.CreateOne(new CreateIndexModel <T>(asc ? Builders <T> .IndexKeys.Ascending(key) : Builders <T> .IndexKeys.Descending(key))));
                }
            }

            return(String.Empty);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="collection">集合名</param>
        /// <param name="index">索引键</param>
        /// <param name="asc"></param>
        /// <returns></returns>
        public string CreateIndex(string collection, string index, bool asc = true)
        {
            IMongoIndexManager <BsonDocument> mgr = Database.GetCollection <BsonDocument>(collection).Indexes;
            var list = mgr.List();

            while (list.MoveNext())
            {
                if (!list.Current.Any(doc => doc["name"].AsString.StartsWith(index)))
                {
                    return(mgr.CreateOne(new CreateIndexModel <BsonDocument>(asc ? Builders <BsonDocument> .IndexKeys.Ascending(doc => doc[index]) : Builders <BsonDocument> .IndexKeys.Descending(doc => doc[index]))));
                }
            }

            return(string.Empty);
        }
        public static void SetIndices <TDocument>(this IMongoIndexManager <TDocument> indexManager, IList <Tuple <IndexKeysDefinition <TDocument>, CreateIndexOptions <TDocument> > > indices) where TDocument : IDocument
        {
            foreach (var index in indices)
            {
                try
                {
                    var indexModel = new CreateIndexModel <TDocument>(index.Item1, index.Item2);

                    indexManager.CreateOne(indexModel);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
Exemplo n.º 4
0
        public static void AddCompletedTaskIndexes(this IMongoIndexManager <CompletedEnglishTaskEntity> indexesConfiguration)
        {
            var index = Builders <CompletedEnglishTaskEntity> .IndexKeys.Ascending(x => x.UserId);

            indexesConfiguration.CreateOne(new CreateIndexModel <CompletedEnglishTaskEntity>(index));
        }
Exemplo n.º 5
0
        private void GetOrCreateCollection <T>()
        {
            Connect();

            string collectionName = typeof(T).Name;

            Logger.Trace($"{nameof(MongoStore)}.{nameof(GetOrCreateCollection)}<{typeof(T).Name}>",
                         new LogItem("Event", "Mongo GetCollection"));

            IMongoCollection <T> collection = Database.GetCollection <T>(collectionName);

            Dictionary <string, List <string> > indexes =
                new Dictionary <string, List <string> > {
                { "ExpiresAt", new List <string> {
                      "ExpiresAt"
                  } }
            };

            PropertyInfo[] members = typeof(T).GetProperties();
            foreach (PropertyInfo memberInfo in members)
            {
                MongoIndexAttribute indexAttribute = memberInfo.GetCustomAttribute <MongoIndexAttribute>();
                if (indexAttribute == null)
                {
                    continue;
                }
                if (!indexes.ContainsKey(indexAttribute.IndexName))
                {
                    indexes.Add(indexAttribute.IndexName, new List <string>());
                }
                indexes[indexAttribute.IndexName].Add(memberInfo.Name);
            }

            IMongoIndexManager <T> indexManager = collection.Indexes;

            foreach (KeyValuePair <string, List <string> > index in indexes)
            {
                bool indexExists = false;

                using (IAsyncCursor <BsonDocument> asyncCursor = indexManager.List())
                    while (asyncCursor.MoveNext() && !indexExists)
                    {
                        indexExists = CheckIndexExists(asyncCursor, index);
                    }

                if (!indexExists)
                {
                    string indexJson = $"{{{string.Join(",", index.Value.Select(field => $"\"{field}\":1"))}}}";
                    Logger.Trace($"{nameof(MongoStore)}.{nameof(GetOrCreateCollection)}<{typeof(T).Name}>",
                                 new LogItem("Action", $"Create ExpiresAt index"));

                    CreateIndexOptions cio = new CreateIndexOptions {
                        Name = index.Key
                    };
                    if (index.Key == "ExpiresAt")
                    {
                        cio.ExpireAfter = TimeSpan.Zero;
                    }

                    indexManager.CreateOne(new JsonIndexKeysDefinition <T>(indexJson), cio);
                }
            }

            CollectionCache.Add(typeof(T), collection);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 更新索引
        /// </summary>
        /// <param name="collection">集合名</param>
        /// <param name="key"></param>
        /// <param name="asc"></param>
        /// <returns></returns>
        public string UpdateIndex <T>(string collection, Expression <Func <T, object> > key, bool asc = true)
        {
            IMongoIndexManager <T> mgr = Database.GetCollection <T>(collection).Indexes;

            return(mgr.CreateOne(new CreateIndexModel <T>(asc ? Builders <T> .IndexKeys.Ascending(key) : Builders <T> .IndexKeys.Descending(key))));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 更新索引
        /// </summary>
        /// <param name="collection">集合名</param>
        /// <param name="index">索引键</param>
        /// <param name="asc"></param>
        /// <returns></returns>
        public string UpdateIndex(string collection, string index, bool asc = true)
        {
            IMongoIndexManager <BsonDocument> mgr = Database.GetCollection <BsonDocument>(collection).Indexes;

            return(mgr.CreateOne(new CreateIndexModel <BsonDocument>(asc ? Builders <BsonDocument> .IndexKeys.Ascending(doc => doc[index]) : Builders <BsonDocument> .IndexKeys.Descending(doc => doc[index]))));
        }