示例#1
0
        /// <summary>
        /// Verify all required <see cref="IMongoIndexKeys"/> are defined and ready to go.
        /// </summary>
        protected virtual void VerifyIndexes <TEntity>(MongoCollection <TEntity> collection)
        {
            Type entityType = typeof(TEntity);

            if (IndexTypesByEntityType.ContainsKey(entityType))
            {
                foreach (object untypedIndexType in IndexTypesByEntityType[entityType])
                {
                    var mongoIndex = (MongoIndex <TEntity>)untypedIndexType;

                    var indexKeysBuilder = new IndexKeysBuilder();
                    if (mongoIndex.IsAcending)
                    {
                        indexKeysBuilder = indexKeysBuilder.Ascending(mongoIndex.Selectors.ToArray());
                    }
                    else
                    {
                        indexKeysBuilder = indexKeysBuilder.Descending(mongoIndex.Selectors.ToArray());
                    }

                    collection.CreateIndex
                    (
                        indexKeysBuilder,
                        IndexOptions
                        .SetUnique(mongoIndex.IsUnique)
                        .SetName(mongoIndex.Name)
                    );
                }
            }
        }
示例#2
0
        protected virtual void VerifyIndexes <TEntity>(IMongoCollection <TEntity> collection)
        {
            Type entityType = typeof(TEntity);

            if (IndexTypesByEntityType.ContainsKey(entityType))
            {
                foreach (object untypedIndexType in IndexTypesByEntityType[entityType])
                {
                    var mongoIndex = (MongoDbIndex <TEntity>)untypedIndexType;

                    IndexKeysDefinitionBuilder <TEntity> indexKeysBuilder = Builders <TEntity> .IndexKeys;
                    IndexKeysDefinition <TEntity>        indexKey         = null;

                    IList <Expression <Func <TEntity, object> > > selectors = mongoIndex.Selectors.ToList();
                    for (int i = 0; i < selectors.Count; i++)
                    {
                        Expression <Func <TEntity, object> > expression = selectors[i];
                        if (mongoIndex.IsAcending)
                        {
                            if (i == 0)
                            {
                                indexKey = indexKeysBuilder.Ascending(expression);
                            }
                            else
                            {
                                indexKey = indexKey.Ascending(expression);
                            }
                        }
                        else
                        {
                            if (i == 0)
                            {
                                indexKey = indexKeysBuilder.Descending(expression);
                            }
                            else
                            {
                                indexKey = indexKey.Descending(expression);
                            }
                        }
                    }

                    collection.Indexes.CreateOne
                    (
                        indexKey,
                        new CreateIndexOptions
                    {
                        Unique = mongoIndex.IsUnique,
                        Name   = mongoIndex.Name
                    }
                    );
                }
            }
        }