using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Create; using MongoDbGenericRepository.DataAccess.Update; using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace MongoDbGenericRepository { /// /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository { private object _initLock; private MongoDbCreator _mongoDbCreator; protected MongoDbCreator MongoDbCreator { get { if (_mongoDbCreator != null) { return _mongoDbCreator; } lock (_initLock) { if (_mongoDbCreator == null) { _mongoDbCreator = new MongoDbCreator(MongoDbContext); } } return _mongoDbCreator; } } private MongoDbUpdater _mongoDbUpdater; protected MongoDbUpdater MongoDbUpdater { get { if (_mongoDbUpdater != null) { return _mongoDbUpdater; } lock (_initLock) { if (_mongoDbUpdater == null) { _mongoDbUpdater = new MongoDbUpdater(MongoDbContext); } } return _mongoDbUpdater; } } /// /// The constructor taking a connection string and a database name. /// /// The connection string of the MongoDb server. /// The name of the database against which you want to perform operations. protected BaseMongoRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName) { } /// /// The contructor taking a . /// /// A mongodb context implementing protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// /// The contructor taking a . /// /// A mongodb context implementing protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) { } #region Create TKey /// /// Asynchronously adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. public virtual async Task AddOneAsync(TDocument document) where TDocument : IDocument where TKey : IEquatable { await MongoDbCreator.AddOneAsync(document); } /// /// Adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. public virtual void AddOne(TDocument document) where TDocument : IDocument where TKey : IEquatable { MongoDbCreator.AddOne(document); } /// /// Asynchronously adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. public virtual async Task AddManyAsync(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { await MongoDbCreator.AddManyAsync(documents); } /// /// Adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. public virtual void AddMany(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { MongoDbCreator.AddMany(documents); } #endregion #region Update /// /// Asynchronously Updates a document. /// /// The type representing a Document. /// The document with the modifications you want to persist. public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(modifiedDocument); } /// /// Updates a document. /// /// The type representing a Document. /// The document with the modifications you want to persist. public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(modifiedDocument); } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The document you want to modify. /// The update definition for the document. public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, update); } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The document you want to modify. /// The update definition for the document. public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(documentToModify, update); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(documentToModify, field, value); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } #endregion Update #region Update TKey /// /// Asynchronously Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document with the modifications you want to persist. public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(modifiedDocument); } /// /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document with the modifications you want to persist. public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(modifiedDocument); } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to modify. /// The update definition for the document. public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(documentToModify, update); } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to modify. /// The update definition for the document. public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(documentToModify, update); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(documentToModify, field, value); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); } /// /// Updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The value of the partition key. public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field. /// The document filter. /// The field selector. /// The new value of the property field. /// The partition key for the document. public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); } #endregion Update #region Delete /// /// Asynchronously deletes a document. /// /// The type representing a Document. /// The document you want to delete. /// The number of documents deleted. public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument { return await DeleteOneAsync(document); } /// /// Deletes a document. /// /// The type representing a Document. /// The document you want to delete. /// The number of documents deleted. public virtual long DeleteOne(TDocument document) where TDocument : IDocument { return DeleteOne(document); } /// /// Deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { return DeleteOne(filter, partitionKey); } /// /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return await DeleteOneAsync(filter, partitionKey); } /// /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter)).DeletedCount; } /// /// Asynchronously deletes a list of documents. /// /// The type representing a Document. /// The list of documents to delete. /// The number of documents deleted. public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument { return await DeleteManyAsync(documents); } /// /// Deletes a list of documents. /// /// The type representing a Document. /// The list of documents to delete. /// The number of documents deleted. public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument { return DeleteMany(documents); } /// /// Deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument { return HandlePartitioned(partitionKey).DeleteMany(filter).DeletedCount; } #endregion Delete #region Delete TKey /// /// Deletes a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to delete. /// The number of documents deleted. public virtual long DeleteOne(TDocument document) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", document.Id); return HandlePartitioned(document).DeleteOne(filter).DeletedCount; } /// /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to delete. /// The number of documents deleted. public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", document.Id); return (await HandlePartitioned(document).DeleteOneAsync(filter)).DeletedCount; } /// /// Deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return HandlePartitioned(partitionKey).DeleteOne(filter).DeletedCount; } /// /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter)).DeletedCount; } /// /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter)).DeletedCount; } /// /// Asynchronously deletes a list of documents. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The list of documents to delete. /// The number of documents deleted. public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { if (!documents.Any()) { return 0; } // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 if (documents.Any(e => e is IPartitionedDocument)) { long deleteCount = 0; foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) { var groupIdsTodelete = group.Select(e => e.Id).ToArray(); deleteCount += (await HandlePartitioned(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsTodelete.Contains(x.Id))).DeletedCount; } return deleteCount; } else { var idsTodelete = documents.Select(e => e.Id).ToArray(); return (await HandlePartitioned(documents.FirstOrDefault()).DeleteManyAsync(x => idsTodelete.Contains(x.Id))).DeletedCount; } } /// /// Deletes a list of documents. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The list of documents to delete. /// The number of documents deleted. public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { if (!documents.Any()) { return 0; } // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 if (documents.Any(e => e is IPartitionedDocument)) { long deleteCount = 0; foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) { var groupIdsTodelete = group.Select(e => e.Id).ToArray(); deleteCount += (HandlePartitioned(group.FirstOrDefault()).DeleteMany(x => groupIdsTodelete.Contains(x.Id))).DeletedCount; } return deleteCount; } else { var idsTodelete = documents.Select(e => e.Id).ToArray(); return (HandlePartitioned(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id))).DeletedCount; } } /// /// Deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. public virtual long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return HandlePartitioned(partitionKey).DeleteMany(filter).DeletedCount; } #endregion #region Project /// /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) .Project(projection) .FirstOrDefaultAsync(); } /// /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) .Project(projection) .FirstOrDefaultAsync(); } /// /// Returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TProjection : class { return HandlePartitioned(partitionKey).Find(filter) .Project(projection) .FirstOrDefault(); } /// /// Returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable where TProjection : class { return HandlePartitioned(partitionKey).Find(filter) .Project(projection) .FirstOrDefault(); } /// /// Asynchronously returns a list of projected documents matching the filter condition. /// /// The type representing a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) .Project(projection) .ToListAsync(); } /// /// Asynchronously returns a list of projected documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) .Project(projection) .ToListAsync(); } /// /// Asynchronously returns a list of projected documents matching the filter condition. /// /// The type representing a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TProjection : class { return HandlePartitioned(partitionKey).Find(filter) .Project(projection) .ToList(); } /// /// Asynchronously returns a list of projected documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. /// The document filter. /// The projection expression. /// An optional partition key. public virtual List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable where TProjection : class { return HandlePartitioned(partitionKey).Find(filter) .Project(projection) .ToList(); } #endregion #region Grouping /// /// Groups a collection of documents given a grouping criteria, /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// /// The type representing a Document. /// The type of the grouping criteria. /// The type of the projected group. /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. public virtual List GroupBy( Expression> groupingCriteria, Expression, TProjection>> groupProjection, string partitionKey = null) where TDocument : IDocument where TProjection : class, new() { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); return collection.Aggregate() .Group(groupingCriteria, groupProjection) .ToList(); } /// /// Groups filtered a collection of documents given a grouping criteria, /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// /// The type representing a Document. /// The type of the grouping criteria. /// The type of the projected group. /// A LINQ expression filter. /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. public virtual List GroupBy(Expression> filter, Expression> selector, Expression, TProjection>> projection, string partitionKey = null) where TDocument : IDocument where TProjection : class, new() { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); return collection.Aggregate() .Match(Builders.Filter.Where(filter)) .Group(selector, projection) .ToList(); } #endregion /// /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// A LINQ expression filter. /// The property selector. /// Order of the sorting. /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. public virtual async Task> GetSortedPaginatedAsync( Expression> filter, Expression> sortSelector, bool ascending = true, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument { var sorting = ascending ? Builders.Sort.Ascending(sortSelector) : Builders.Sort.Descending(sortSelector); return await HandlePartitioned(partitionKey) .Find(filter) .Sort(sorting) .Skip(skipNumber) .Limit(takeNumber) .ToListAsync(); } /// /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// A LINQ expression filter. /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. public virtual async Task> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument { return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); } /// /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. public virtual async Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); } #region Find And Update /// /// GetAndUpdateOne with filter /// /// The type representing a Document. /// A LINQ expression filter. /// /// /// public virtual async Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) where TDocument : IDocument { return await GetCollection().FindOneAndUpdateAsync(filter, update, options); } /// /// GetAndUpdateOne with filter /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// /// /// public virtual async Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) where TDocument : IDocument where TKey : IEquatable { return await GetCollection().FindOneAndUpdateAsync(filter, update, options); } #endregion Find And Update /// /// Sets the value of the document Id if it is not set already. /// /// The document type. /// The type of the primary key. /// The document. protected void FormatDocument(TDocument document) where TDocument : IDocument where TKey : IEquatable { if (document == null) { throw new ArgumentNullException(nameof(document)); } var defaultTKey = default(TKey); if (document.Id == null || (defaultTKey != null && defaultTKey.Equals(document.Id))) { document.Id = IdGenerator.GetId(); } } /// /// Sets the value of the document Id if it is not set already. /// /// The document type. /// The document. protected void FormatDocument(TDocument document) where TDocument : IDocument { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (document.Id == default(Guid)) { document.Id = Guid.NewGuid(); } } } }