/// <summary> /// Creates a get one query against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="database">The database.</param> /// <returns>A new get one query.</returns> public static GetOneQueryUsingFilterDefinitionAsync <TModel> GetOneUsingFilterDefinitionAsync <TModel>(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } GetOneQueryUsingFilterDefinitionAsync <TModel> queryAsync = async(filterDefinition, collectionName, cancellationToken) => { if (filterDefinition == null) { throw new ArgumentNullException(nameof(filterDefinition)); } var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var client = database.CreateClient(); var collection = client.GetCollection <TModel>(modelTypeName); var results = await collection.Find(filterDefinition).ToListAsync(cancellationToken); return(results.SingleOrDefault()); }; return(queryAsync); }
/// <summary> /// Creates a project all query against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TProjection">The type of the projection.</typeparam> /// <param name="database">The database.</param> /// <returns>A new project all query.</returns> public static ProjectAllQueryAsync <TModel, TProjection> ProjectAllAsync <TModel, TProjection>(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } ProjectAllQueryAsync <TModel, TProjection> queryAsync = async(project, collectionName, cancellationToken) => { if (project == null) { throw new ArgumentNullException((nameof(project))); } var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var client = database.CreateClient(); var collection = client.GetCollection <TModel>(modelTypeName); var projectionDefinition = Builders <TModel> .Projection.Expression(project); var filter = new BsonDocument(); var findOptions = new FindOptions <TModel, TProjection>() { Projection = projectionDefinition }; var findResults = await collection.FindAsync(filter, findOptions, cancellationToken); return(await findResults.ToListAsync(cancellationToken)); }; return(queryAsync); }
/// <summary> /// Executes a query to replace many models (for Update or AddOrUpdate many). /// </summary> /// <typeparam name="TId">The type of the identifier.</typeparam> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="database">The database.</param> /// <param name="models">The set of models to write.</param> /// <param name="collectionName">Name of the collection.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="isUpsert"> /// If set to <c>true</c> models will be defined as upserts (AddOrUpdate); otherwise they /// will be defined as updates. /// </param> /// <returns>The bulk write task results.</returns> /// <exception cref="System.ArgumentNullException">If models is null.</exception> private static async Task <BulkWriteResult <BsonDocument> > ReplaceManyAsync <TId, TModel>( MongoDatabase database, IDictionary <TId, TModel> models, string collectionName, CancellationToken cancellationToken, bool isUpsert) { if (models == null) { throw new ArgumentNullException(nameof(models)); } var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var client = database.CreateClient(); var collection = client.GetCollection <BsonDocument>(modelTypeName); var updateModels = new List <WriteModel <BsonDocument> >(); foreach (var keyValue in models) { var bsonDoc = keyValue.Value.ToBsonDocument(); var filter = new BsonDocument("_id", BsonValue.Create(keyValue.Key)); var replaceOne = new ReplaceOneModel <BsonDocument>(filter, bsonDoc) { IsUpsert = isUpsert }; updateModels.Add(replaceOne); } var results = await collection.BulkWriteAsync(updateModels, null, cancellationToken); return(results); }
/// <summary> /// Creates an add many command against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="database">The database.</param> /// <returns>A new add many command./</returns> public static AddManyCommandAsync <TModel> AddManyAsync <TModel>(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } AddManyCommandAsync <TModel> commandAsync = async(models, collectionName, cancellationToken) => { var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var client = database.CreateClient(); var collection = client.GetCollection <TModel>(modelTypeName); try { await collection.InsertManyAsync(models, null, cancellationToken); } catch (MongoException ex) { throw new DatabaseException($"Unable to add {nameof(models)} of type {typeof(TModel).Name} to database.", ex); } }; return(commandAsync); }
/// <summary> /// Creates an add or update one command against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="readModelDatabase">The database.</param> /// <returns>A new add or update one command.</returns> public static AddOrUpdateOneCommandAsync <TModel> AddOrUpdateOneAsync <TModel>(MongoDatabase readModelDatabase) { if (readModelDatabase == null) { throw new ArgumentNullException(nameof(readModelDatabase)); } AddOrUpdateOneCommandAsync <TModel> commandAsync = async(model, collectionName, cancellationToken) => { if (model == null) { throw new ArgumentNullException(nameof(model)); } var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var updateOptions = new UpdateOptions { IsUpsert = true }; var id = IdReader.ReadValue(model); var filter = Builders <TModel> .Filter.Eq("_id", id); var client = readModelDatabase.CreateClient(); var collection = client.GetCollection <TModel>(modelTypeName); await collection.ReplaceOneAsync(filter, model, updateOptions, cancellationToken); }; return(commandAsync); }
/// <summary> /// Creates a merge complete set command against the specified database. /// </summary> /// <typeparam name="TId">The type of the identifier.</typeparam> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TMetadata">The type of the metadata.</typeparam> /// <param name="database">The database.</param> /// <returns>A new merge complete set command.</returns> public static MergeCompleteSetCommandAsync <TId, TModel, TMetadata> MergeCompleteSetAsync <TId, TModel, TMetadata>( MongoDatabase database) { // remove from set b where not in set a var addOrUpdateManyCommandAsync = AddOrUpdateManyAsync <TId, TModel, TMetadata>(database); MergeCompleteSetCommandAsync <TId, TModel, TMetadata> commandAsync = async(models, collectionName, cancellationToken) => { if (models == null) { throw new ArgumentNullException(nameof(models)); } var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; // Remove all existing models with ids not in the list of ids given var equalFilter = Builders <StorageModel <TModel, TMetadata> > .Filter.In( "_id", models.Select(kvp => IdReader.ReadValue(kvp.Value.Model))); var notEqualFilter = Builders <StorageModel <TModel, TMetadata> > .Filter.Not(equalFilter); var client = database.CreateClient(); var collection = client.GetCollection <StorageModel <TModel, TMetadata> >(modelTypeName); await collection.DeleteManyAsync(notEqualFilter, cancellationToken); // Add or update the rest await addOrUpdateManyCommandAsync(models, modelTypeName, cancellationToken); }; return(commandAsync); }
/// <summary> /// Creates an update one command against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TMetadata">The type of the metadata.</typeparam> /// <param name="database">The database.</param> /// <returns>A new update one command.</returns> public static UpdateOneCommandAsync <TModel, TMetadata> UpdateOneAsync <TModel, TMetadata>(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } UpdateOneCommandAsync <TModel, TMetadata> commandAsync = async(model, metadata, collectionName, cancellationToken) => { if (model == null) { throw new ArgumentNullException(nameof(model)); } var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var id = IdReader.ReadValue(model); var storageModel = new StorageModel <TModel, TMetadata> { Id = id, Model = model, Metadata = metadata }; var updateOptions = new UpdateOptions { IsUpsert = false }; var filter = Builders <StorageModel <TModel, TMetadata> > .Filter.Eq("model._id", id); var client = database.CreateClient(); var collection = client.GetCollection <StorageModel <TModel, TMetadata> >(modelTypeName); var result = await collection.ReplaceOneAsync(filter, storageModel, updateOptions, cancellationToken); if (result.ModifiedCount == 0) { throw new DatabaseException( $"Unable to find {nameof(model)} of type {typeof(TModel).Name} with id '{id}' in data store to update."); } }; return(commandAsync); }
/// <summary> /// Creates a remove many command against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="database">The database.</param> /// <returns>A new remove many command.</returns> public static RemoveManyCommandUsingFilterDefinitionAsync <TModel> RemoveManyUsingFilterDefinitionAsync <TModel>(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } RemoveManyCommandUsingFilterDefinitionAsync <TModel> commandAsync = async(filterDefinition, collectionName, cancellationToken) => { var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var client = database.CreateClient(); var collection = client.GetCollection <TModel>(modelTypeName); await collection.DeleteManyAsync(filterDefinition, cancellationToken); }; return(commandAsync); }
/// <summary> /// Creates a get all query against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="database">The database.</param> /// <returns>A new get all query.</returns> public static GetAllQueryAsync <TModel> GetAllAsync <TModel>(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } GetAllQueryAsync <TModel> queryAsync = async(collectionName, cancellationToken) => { var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var client = database.CreateClient(); var collection = client.GetCollection <TModel>(modelTypeName); var filter = new BsonDocument(); return(await collection.Find(filter).ToListAsync(cancellationToken)); }; return(queryAsync); }
/// <summary> /// Creates a remove all command against the specified database. /// </summary> /// <param name="database">The database.</param> /// <returns>A new remove all command.</returns> public static RemoveAllCommandAsync RemoveAllAsync(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } RemoveAllCommandAsync commandAsync = async(collectionName, cancellationToken) => { if (string.IsNullOrWhiteSpace(collectionName)) { throw new ArgumentNullException(nameof(collectionName)); } var client = database.CreateClient(); await client.DropCollectionAsync(collectionName, cancellationToken); }; return(commandAsync); }
/// <summary> /// Creates a remove one command against the specified database. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="database">The database.</param> /// <returns>A new remove one command.</returns> public static RemoveOneCommandAsync <TModel> RemoveOneAsync <TModel>(MongoDatabase database) { if (database == null) { throw new ArgumentNullException(nameof(database)); } RemoveOneCommandAsync <TModel> commandAsync = async(model, collectionName, cancellationToken) => { var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName; var filter = Builders <TModel> .Filter.Eq("_id", IdReader.ReadValue(model)); var client = database.CreateClient(); var collection = client.GetCollection <TModel>(modelTypeName); await collection.DeleteOneAsync(filter, cancellationToken); }; return(commandAsync); }