/// <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)); }
public void Drop <T>() { try { _provider.Database.DropCollection(MongoConfiguration.GetCollectionName(typeof(T))); } catch (MongoException) { } }
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); }
/// <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); }
/// <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); }
public void Drop <T>() { _provider.Database.DropCollection(MongoConfiguration.GetCollectionName(typeof(T))); }
/// <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))) { }
/// <summary> /// Initializes a new instance of the <see cref="MapReduceOptions<T>"/> class. /// </summary> public MapReduceOptions() { CollectionName = MongoConfiguration.GetCollectionName(typeof(T)); }
public void Can_correctly_determine_collection_name_from_discriminated_sub_class_when_fluent_mapped() { var collectionName = MongoConfiguration.GetCollectionName(typeof(SubClassedObjectFluentMapped)); Assert.Equal("SuperClassObjectFluentMapped", collectionName); }
public void Can_correctly_determine_collection_name_when_discriminator_is_on_an_interface() { var collectionName = MongoConfiguration.GetCollectionName(typeof(InterfaceDiscriminatedClass)); Assert.Equal("IDiscriminated", collectionName); }
public void Can_correctly_determine_collection_name() { var collectionName = MongoConfiguration.GetCollectionName(typeof(SuperClassObject)); Assert.Equal("SuperClassObject", collectionName); }