/* Copyright 2010-2016 MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Linq;
namespace MongoDB.Driver
{
///
/// Extension methods for .
///
public static class IMongoCollectionExtensions
{
///
/// Begins a fluent aggregation interface.
///
/// The type of the document.
/// The collection.
/// The options.
///
/// A fluent aggregate interface.
///
public static IAggregateFluent Aggregate(this IMongoCollection collection, AggregateOptions options = null)
{
var emptyPipeline = new EmptyPipelineDefinition(collection.DocumentSerializer);
return new AggregateFluent(collection, emptyPipeline, options ?? new AggregateOptions());
}
///
/// Creates a queryable source of documents.
///
/// The type of the document.
/// The collection.
/// The aggregate options
/// A queryable source of documents.
public static IMongoQueryable AsQueryable(this IMongoCollection collection, AggregateOptions aggregateOptions = null)
{
Ensure.IsNotNull(collection, nameof(collection));
aggregateOptions = aggregateOptions ?? new AggregateOptions();
var provider = new MongoQueryProviderImpl(collection, aggregateOptions);
return new MongoQueryableImpl(provider);
}
///
/// Counts the number of documents in the collection.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The number of documents in the collection.
///
public static long Count(this IMongoCollection collection, Expression> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.Count(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Counts the number of documents in the collection.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The number of documents in the collection.
///
public static Task CountAsync(this IMongoCollection collection, Expression> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.CountAsync(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Deletes multiple documents.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static DeleteResult DeleteMany(this IMongoCollection collection, Expression> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return collection.DeleteMany(filter, null, cancellationToken);
}
///
/// Deletes multiple documents.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static DeleteResult DeleteMany(this IMongoCollection collection, Expression> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.DeleteMany(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Deletes multiple documents.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static Task DeleteManyAsync(this IMongoCollection collection, Expression> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return collection.DeleteManyAsync(filter, null, cancellationToken);
}
///
/// Deletes multiple documents.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static Task DeleteManyAsync(this IMongoCollection collection, Expression> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.DeleteManyAsync(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Deletes a single document.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static DeleteResult DeleteOne(this IMongoCollection collection, Expression> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return collection.DeleteOne(filter, null, cancellationToken);
}
///
/// Deletes a single document.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static DeleteResult DeleteOne(this IMongoCollection collection, Expression> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.DeleteOne(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Deletes a single document.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static Task DeleteOneAsync(this IMongoCollection collection, Expression> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return collection.DeleteOneAsync(filter, null, cancellationToken);
}
///
/// Deletes a single document.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The result of the delete operation.
///
public static Task DeleteOneAsync(this IMongoCollection collection, Expression> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.DeleteOneAsync(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Gets the distinct values for a specified field.
///
/// The type of the document.
/// The type of the result.
/// The collection.
/// The field.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The distinct values for the specified field.
///
public static IAsyncCursor Distinct(this IMongoCollection collection, Expression> field, FilterDefinition filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(filter, nameof(filter));
return collection.Distinct(
new ExpressionFieldDefinition(field),
filter,
options,
cancellationToken);
}
///
/// Gets the distinct values for a specified field.
///
/// The type of the document.
/// The type of the result.
/// The collection.
/// The field.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The distinct values for the specified field.
///
public static IAsyncCursor Distinct(this IMongoCollection collection, FieldDefinition field, Expression> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(filter, nameof(filter));
return collection.Distinct(
field,
new ExpressionFilterDefinition(filter),
options,
cancellationToken);
}
///
/// Gets the distinct values for a specified field.
///
/// The type of the document.
/// The type of the result.
/// The collection.
/// The field.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The distinct values for the specified field.
///
public static IAsyncCursor Distinct(this IMongoCollection collection, Expression> field, Expression> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(filter, nameof(filter));
return collection.Distinct(
new ExpressionFieldDefinition(field),
new ExpressionFilterDefinition(filter),
options,
cancellationToken);
}
///
/// Gets the distinct values for a specified field.
///
/// The type of the document.
/// The type of the result.
/// The collection.
/// The field.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The distinct values for the specified field.
///
public static Task> DistinctAsync(this IMongoCollection collection, Expression> field, FilterDefinition filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(filter, nameof(filter));
return collection.DistinctAsync(
new ExpressionFieldDefinition(field),
filter,
options,
cancellationToken);
}
///
/// Gets the distinct values for a specified field.
///
/// The type of the document.
/// The type of the result.
/// The collection.
/// The field.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The distinct values for the specified field.
///
public static Task> DistinctAsync(this IMongoCollection collection, FieldDefinition field, Expression> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(filter, nameof(filter));
return collection.DistinctAsync(
field,
new ExpressionFilterDefinition(filter),
options,
cancellationToken);
}
///
/// Gets the distinct values for a specified field.
///
/// The type of the document.
/// The type of the result.
/// The collection.
/// The field.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The distinct values for the specified field.
///
public static Task> DistinctAsync(this IMongoCollection collection, Expression> field, Expression> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(filter, nameof(filter));
return collection.DistinctAsync(
new ExpressionFieldDefinition(field),
new ExpressionFilterDefinition(filter),
options,
cancellationToken);
}
///
/// Begins a fluent find interface.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
///
/// A fluent find interface.
///
public static IFindFluent Find(this IMongoCollection collection, FilterDefinition filter, FindOptions options = null)
{
FindOptions genericOptions;
if (options == null)
{
genericOptions = new FindOptions();
}
else
{
genericOptions = new FindOptions
{
AllowPartialResults = options.AllowPartialResults,
BatchSize = options.BatchSize,
Collation = options.Collation,
Comment = options.Comment,
CursorType = options.CursorType,
MaxAwaitTime = options.MaxAwaitTime,
MaxTime = options.MaxTime,
Modifiers = options.Modifiers,
NoCursorTimeout = options.NoCursorTimeout,
OplogReplay = options.OplogReplay
};
}
return new FindFluent(collection, filter, genericOptions);
}
///
/// Begins a fluent find interface.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
///
/// A fluent interface.
///
public static IFindFluent Find(this IMongoCollection collection, Expression> filter, FindOptions options = null)
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.Find(new ExpressionFilterDefinition(filter), options);
}
///
/// Finds the documents matching the filter.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
/// A Task whose result is a cursor.
public static IAsyncCursor FindSync(this IMongoCollection collection, FilterDefinition filter, FindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindSync(filter, options, cancellationToken);
}
///
/// Finds the documents matching the filter.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
/// A Task whose result is a cursor.
public static IAsyncCursor FindSync(this IMongoCollection collection, Expression> filter, FindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindSync(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Finds the documents matching the filter.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
/// A Task whose result is a cursor.
public static Task> FindAsync(this IMongoCollection collection, FilterDefinition filter, FindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindAsync(filter, options, cancellationToken);
}
///
/// Finds the documents matching the filter.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
/// A Task whose result is a cursor.
public static Task> FindAsync(this IMongoCollection collection, Expression> filter, FindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindAsync(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Finds a single document and deletes it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The deleted document if one was deleted.
///
public static TDocument FindOneAndDelete(this IMongoCollection collection, FilterDefinition filter, FindOneAndDeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndDelete(filter, options, cancellationToken);
}
///
/// Finds a single document and deletes it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The deleted document if one was deleted.
///
public static TDocument FindOneAndDelete(this IMongoCollection collection, Expression> filter, FindOneAndDeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndDelete(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Finds a single document and deletes it atomically.
///
/// The type of the document.
/// The type of the projection (same as TDocument if there is no projection).
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static TProjection FindOneAndDelete(this IMongoCollection collection, Expression> filter, FindOneAndDeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndDelete(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Finds a single document and deletes it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The deleted document if one was deleted.
///
public static Task FindOneAndDeleteAsync(this IMongoCollection collection, FilterDefinition filter, FindOneAndDeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndDeleteAsync(filter, options, cancellationToken);
}
///
/// Finds a single document and deletes it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The deleted document if one was deleted.
///
public static Task FindOneAndDeleteAsync(this IMongoCollection collection, Expression> filter, FindOneAndDeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndDeleteAsync(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Finds a single document and deletes it atomically.
///
/// The type of the document.
/// The type of the projection (same as TDocument if there is no projection).
/// The collection.
/// The filter.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static Task FindOneAndDeleteAsync(this IMongoCollection collection, Expression> filter, FindOneAndDeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndDeleteAsync(new ExpressionFilterDefinition(filter), options, cancellationToken);
}
///
/// Finds a single document and replaces it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The replacement.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static TDocument FindOneAndReplace(this IMongoCollection collection, FilterDefinition filter, TDocument replacement, FindOneAndReplaceOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndReplace(filter, replacement, options, cancellationToken);
}
///
/// Finds a single document and replaces it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The replacement.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static TDocument FindOneAndReplace(this IMongoCollection collection, Expression> filter, TDocument replacement, FindOneAndReplaceOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndReplace(new ExpressionFilterDefinition(filter), replacement, options, cancellationToken);
}
///
/// Finds a single document and replaces it atomically.
///
/// The type of the document.
/// The type of the projection (same as TDocument if there is no projection).
/// The collection.
/// The filter.
/// The replacement.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static TProjection FindOneAndReplace(this IMongoCollection collection, Expression> filter, TDocument replacement, FindOneAndReplaceOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndReplace(filter, replacement, options, cancellationToken);
}
///
/// Finds a single document and replaces it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The replacement.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static Task FindOneAndReplaceAsync(this IMongoCollection collection, FilterDefinition filter, TDocument replacement, FindOneAndReplaceOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndReplaceAsync(filter, replacement, options, cancellationToken);
}
///
/// Finds a single document and replaces it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The replacement.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static Task FindOneAndReplaceAsync(this IMongoCollection collection, Expression> filter, TDocument replacement, FindOneAndReplaceOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndReplaceAsync(new ExpressionFilterDefinition(filter), replacement, options, cancellationToken);
}
///
/// Finds a single document and replaces it atomically.
///
/// The type of the document.
/// The type of the projection (same as TDocument if there is no projection).
/// The collection.
/// The filter.
/// The replacement.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static Task FindOneAndReplaceAsync(this IMongoCollection collection, Expression> filter, TDocument replacement, FindOneAndReplaceOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.FindOneAndReplaceAsync(filter, replacement, options, cancellationToken);
}
///
/// Finds a single document and updates it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The update.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static TDocument FindOneAndUpdate(this IMongoCollection collection, FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
Ensure.IsNotNull(update, nameof(update));
return collection.FindOneAndUpdate(
filter,
update,
options,
cancellationToken);
}
///
/// Finds a single document and updates it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The update.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static TDocument FindOneAndUpdate(this IMongoCollection collection, Expression> filter, UpdateDefinition update, FindOneAndUpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
Ensure.IsNotNull(update, nameof(update));
return collection.FindOneAndUpdate(
new ExpressionFilterDefinition(filter),
update,
options,
cancellationToken);
}
///
/// Finds a single document and updates it atomically.
///
/// The type of the document.
/// The type of the projection (same as TDocument if there is no projection).
/// The collection.
/// The filter.
/// The update.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static TProjection FindOneAndUpdate(this IMongoCollection collection, Expression> filter, UpdateDefinition update, FindOneAndUpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
Ensure.IsNotNull(update, nameof(update));
return collection.FindOneAndUpdate(new ExpressionFilterDefinition(filter), update, options, cancellationToken);
}
///
/// Finds a single document and updates it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The update.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static Task FindOneAndUpdateAsync(this IMongoCollection collection, FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
Ensure.IsNotNull(update, nameof(update));
return collection.FindOneAndUpdateAsync(
filter,
update,
options,
cancellationToken);
}
///
/// Finds a single document and updates it atomically.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The update.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static Task FindOneAndUpdateAsync(this IMongoCollection collection, Expression> filter, UpdateDefinition update, FindOneAndUpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
Ensure.IsNotNull(update, nameof(update));
return collection.FindOneAndUpdateAsync(
new ExpressionFilterDefinition(filter),
update,
options,
cancellationToken);
}
///
/// Finds a single document and updates it atomically.
///
/// The type of the document.
/// The type of the projection (same as TDocument if there is no projection).
/// The collection.
/// The filter.
/// The update.
/// The options.
/// The cancellation token.
///
/// The returned document.
///
public static Task FindOneAndUpdateAsync(this IMongoCollection collection, Expression> filter, UpdateDefinition update, FindOneAndUpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
Ensure.IsNotNull(update, nameof(update));
return collection.FindOneAndUpdateAsync(new ExpressionFilterDefinition(filter), update, options, cancellationToken);
}
///
/// Replaces a single document.
///
/// The type of the document.
/// The collection.
/// The filter.
/// The replacement.
/// The options.
/// The cancellation token.
///
/// The result of the replacement.
///
public static ReplaceOneResult ReplaceOne(this IMongoCollection collection, Expression> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(collection, nameof(collection));
Ensure.IsNotNull(filter, nameof(filter));
return collection.ReplaceOne(new ExpressionFilterDefinition