Пример #1
0
        /// <summary>
        /// Gets a collection.
        /// </summary>
        /// <typeparam retval="T">Collection type</typeparam>
        /// <returns></returns>
        public IMongoCollection <T> GetCollection <T>()
        {
            // return new MongoCollection<T>(typeof (T).Name, this, _connection);
            var collectionName = MongoConfiguration.GetCollectionName(typeof(T));

            return(GetCollection <T>(collectionName));
        }
Пример #2
0
 public void Drop <T>()
 {
     try
     {
         _provider.Database.DropCollection(MongoConfiguration.GetCollectionName(typeof(T)));
     }
     catch (MongoException)
     {
     }
 }
Пример #3
0
        public T MapReduce <T>(string map, string reduce)
        {
            T         result = default(T);
            MapReduce mr     = _provider.Database.CreateMapReduce();

            MapReduceResponse response =
                mr.Execute(new MapReduceOptions(MongoConfiguration.GetCollectionName(typeof(T)))
            {
                Map    = map,
                Reduce = reduce
            });
            IMongoCollection <MapReduceResult <T> > coll = response.GetCollection <MapReduceResult <T> >();
            MapReduceResult <T> r = coll.Find().FirstOrDefault();

            result = r.Value;

            return(result);
        }
Пример #4
0
        /// <summary>
        /// Creates the seeds asynchronous.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task CreateSeedsAsync(IMongoDatabase database, MongoConfiguration configuration, CancellationToken cancellationToken = default)
        {
            var collectionName = configuration.GetCollectionName <TEntity>();
            var collection     = database.GetCollection <TEntity>(collectionName);

            var tasks = _seeds.Select(async seed =>
            {
                var filter = GetKeyFilter(seed);
                var cursor = await collection.FindAsync(filter, cancellationToken: cancellationToken);
                if (await cursor.AnyAsync(cancellationToken))
                {
                    return;
                }

                await collection.InsertOneAsync(seed, cancellationToken: cancellationToken);
            });
            await Task.WhenAll(tasks);
        }
Пример #5
0
        /// <summary>
        /// Creates the configured indexes using the specified mongo database and configuration.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task CreateIndexesAsync(IMongoDatabase database, MongoConfiguration configuration, CancellationToken cancellationToken = default)
        {
            var collectionName = configuration.GetCollectionName <TEntity>();
            var collection     = database.GetCollection <TEntity>(collectionName);

            var tasks = _indexes.Select(async index =>
            {
                try
                {
                    await collection.Indexes.CreateOneAsync(index.Keys, index.Options, cancellationToken);
                }
                catch (MongoCommandException)
                {
                    // Drop and try again.
                    await collection.Indexes.DropOneAsync(index.Options.Name, cancellationToken);
                    await collection.Indexes.CreateOneAsync(index.Keys, index.Options, cancellationToken);
                }
            });
            await Task.WhenAll(tasks);
        }
Пример #6
0
 public void Drop <T>()
 {
     _provider.Database.DropCollection(MongoConfiguration.GetCollectionName(typeof(T)));
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QueryMessage{T}"/> class.
 /// </summary>
 /// <param name="connection">
 /// The connection.
 /// </param>
 public QueryMessage(IConnection connection) : base(connection, MongoConfiguration.GetCollectionName(typeof(T)))
 {
 }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MapReduceOptions&lt;T&gt;"/> class.
 /// </summary>
 public MapReduceOptions()
 {
     CollectionName = MongoConfiguration.GetCollectionName(typeof(T));
 }
Пример #9
0
        public void Can_correctly_determine_collection_name_from_discriminated_sub_class_when_fluent_mapped()
        {
            var collectionName = MongoConfiguration.GetCollectionName(typeof(SubClassedObjectFluentMapped));

            Assert.Equal("SuperClassObjectFluentMapped", collectionName);
        }
Пример #10
0
        public void Can_correctly_determine_collection_name_when_discriminator_is_on_an_interface()
        {
            var collectionName = MongoConfiguration.GetCollectionName(typeof(InterfaceDiscriminatedClass));

            Assert.Equal("IDiscriminated", collectionName);
        }
Пример #11
0
        public void Can_correctly_determine_collection_name()
        {
            var collectionName = MongoConfiguration.GetCollectionName(typeof(SuperClassObject));

            Assert.Equal("SuperClassObject", collectionName);
        }