Table of Contents

Class UpdateAndGet<T, TProjection>

Namespace
MongoDB.Entities
Assembly
MongoDB.Entities.dll

Update and retrieve the first document that was updated.

TIP: Specify a filter first with the .Match(). Then set property values with .Modify() and finally call .Execute() to run the command.

public class UpdateAndGet<T, TProjection> : UpdateBase<T> where T : IEntity

Type Parameters

T

Any class that implements IEntity

TProjection

The type to project to

Inheritance
UpdateAndGet<T, TProjection>
Derived
Inherited Members

Methods

ExecuteAsync(CancellationToken)

Run the update command in MongoDB and retrieve the first document modified

public Task<TProjection?> ExecuteAsync(CancellationToken cancellation = default)

Parameters

cancellation CancellationToken

An optional cancellation token

Returns

Task<TProjection>

ExecutePipelineAsync(CancellationToken)

Run the update command with pipeline stages and retrieve the first document modified

public Task<TProjection?> ExecutePipelineAsync(CancellationToken cancellation = default)

Parameters

cancellation CancellationToken

An optional cancellation token

Returns

Task<TProjection>

IgnoreGlobalFilters()

Specify that this operation should ignore any global filters

public UpdateAndGet<T, TProjection> IgnoreGlobalFilters()

Returns

UpdateAndGet<T, TProjection>

IncludeRequiredProps()

Specify to automatically include all properties marked with [BsonRequired] attribute on the entity in the final projection.

HINT: this method should only be called after the .Project() method.

public UpdateAndGet<T, TProjection> IncludeRequiredProps()

Returns

UpdateAndGet<T, TProjection>

Match(FilterDefinition<T>)

Specify the matching criteria with a filter definition

public UpdateAndGet<T, TProjection> Match(FilterDefinition<T> filterDefinition)

Parameters

filterDefinition FilterDefinition<T>

A filter definition

Returns

UpdateAndGet<T, TProjection>

Match(Search, string, bool, bool, string?)

Specify a search term to find results from the text index of this particular collection.

TIP: Make sure to define a text index with DB.Index<T>() before searching

public UpdateAndGet<T, TProjection> Match(Search searchType, string searchTerm, bool caseSensitive = false, bool diacriticSensitive = false, string? language = null)

Parameters

searchType Search

The type of text matching to do

searchTerm string

The search term

caseSensitive bool

Case sensitivity of the search (optional)

diacriticSensitive bool

Diacritic sensitivity of the search (optional)

language string

The language for the search (optional)

Returns

UpdateAndGet<T, TProjection>

Match(Template)

Specify the matching criteria with a template

public UpdateAndGet<T, TProjection> Match(Template template)

Parameters

template Template

A Template with a find query

Returns

UpdateAndGet<T, TProjection>

Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>)

Specify the matching criteria with a filter expression

public UpdateAndGet<T, TProjection> Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter)

Parameters

filter Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>

f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value)

Returns

UpdateAndGet<T, TProjection>

Match(Expression<Func<T, bool>>)

Specify the matching criteria with a lambda expression

public UpdateAndGet<T, TProjection> Match(Expression<Func<T, bool>> expression)

Parameters

expression Expression<Func<T, bool>>

x => x.Property == Value

Returns

UpdateAndGet<T, TProjection>

Match(Expression<Func<T, object?>>, Coordinates2D, double?, double?)

Specify criteria for matching entities based on GeoSpatial data (longitude & latitude)

TIP: Make sure to define a Geo2DSphere index with DB.Index<T>() before searching

Note: DB.FluentGeoNear() supports more advanced options

public UpdateAndGet<T, TProjection> Match(Expression<Func<T, object?>> coordinatesProperty, Coordinates2D nearCoordinates, double? maxDistance = null, double? minDistance = null)

Parameters

coordinatesProperty Expression<Func<T, object>>

The property where 2DCoordinates are stored

nearCoordinates Coordinates2D

The search point

maxDistance double?

Maximum distance in meters from the search point

minDistance double?

Minimum distance in meters from the search point

Returns

UpdateAndGet<T, TProjection>

MatchExpression(Template)

Specify the matching criteria with a Template

public UpdateAndGet<T, TProjection> MatchExpression(Template template)

Parameters

template Template

A Template object

Returns

UpdateAndGet<T, TProjection>

MatchExpression(string)

Specify the matching criteria with an aggregation expression (i.e. $expr)

public UpdateAndGet<T, TProjection> MatchExpression(string expression)

Parameters

expression string

{ $gt: ['$Property1', '$Property2'] }

Returns

UpdateAndGet<T, TProjection>

MatchID(object)

Specify an IEntity ID as the matching criteria

public UpdateAndGet<T, TProjection> MatchID(object ID)

Parameters

ID object

A unique IEntity ID

Returns

UpdateAndGet<T, TProjection>

MatchString(string)

Specify the matching criteria with a JSON string

public UpdateAndGet<T, TProjection> MatchString(string jsonString)

Parameters

jsonString string

{ Title : 'The Power Of Now' }

Returns

UpdateAndGet<T, TProjection>

Modify(Template)

Specify an update with a Template to modify the Entities (use multiple times if needed)

public UpdateAndGet<T, TProjection> Modify(Template template)

Parameters

template Template

A Template with a single update

Returns

UpdateAndGet<T, TProjection>

Modify(Func<UpdateDefinitionBuilder<T>, UpdateDefinition<T>>)

Specify the update definition builder operation to modify the Entities (use multiple times if needed)

public UpdateAndGet<T, TProjection> Modify(Func<UpdateDefinitionBuilder<T>, UpdateDefinition<T>> operation)

Parameters

operation Func<UpdateDefinitionBuilder<T>, UpdateDefinition<T>>

b => b.Inc(x => x.PropName, Value)

Returns

UpdateAndGet<T, TProjection>

Modify(string)

Specify an update (json string) to modify the Entities (use multiple times if needed)

public UpdateAndGet<T, TProjection> Modify(string update)

Parameters

update string

{ \(set: { 'RootProp.\)[x].SubProp' : 321 } }

Returns

UpdateAndGet<T, TProjection>

ModifyExcept(Expression<Func<T, object?>>, T)

Modify all EXCEPT the specified properties with the values from a given entity instance.

public UpdateAndGet<T, TProjection> ModifyExcept(Expression<Func<T, object?>> members, T entity)

Parameters

members Expression<Func<T, object>>

Supply a new expression with the properties to exclude. Ex: x => new { x.Prop1, x.Prop2 }

entity T

The entity instance to read the corresponding values from

Returns

UpdateAndGet<T, TProjection>

ModifyOnly(Expression<Func<T, object?>>, T)

Modify ONLY the specified properties with the values from a given entity instance.

public UpdateAndGet<T, TProjection> ModifyOnly(Expression<Func<T, object?>> members, T entity)

Parameters

members Expression<Func<T, object>>

A new expression with the properties to include. Ex: x => new { x.PropOne, x.PropTwo }

entity T

The entity instance to read the corresponding values from

Returns

UpdateAndGet<T, TProjection>

ModifyWith(T)

Modify ALL properties with the values from the supplied entity instance.

public UpdateAndGet<T, TProjection> ModifyWith(T entity)

Parameters

entity T

The entity instance to read the property values from

Returns

UpdateAndGet<T, TProjection>

Modify<TProp>(Expression<Func<T, TProp>>, TProp)

Specify the property and it's value to modify (use multiple times if needed)

public UpdateAndGet<T, TProjection> Modify<TProp>(Expression<Func<T, TProp>> property, TProp value)

Parameters

property Expression<Func<T, TProp>>

x => x.Property

value TProp

The value to set on the property

Returns

UpdateAndGet<T, TProjection>

Type Parameters

TProp

Option(Action<FindOneAndUpdateOptions<T, TProjection>>)

Specify an option for this update command (use multiple times if needed)

TIP: Setting options is not required

public UpdateAndGet<T, TProjection> Option(Action<FindOneAndUpdateOptions<T, TProjection>> option)

Parameters

option Action<FindOneAndUpdateOptions<T, TProjection>>

x => x.OptionName = OptionValue

Returns

UpdateAndGet<T, TProjection>

Project(Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>>)

Specify how to project the results using a projection expression

public UpdateAndGet<T, TProjection> Project(Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>> projection)

Parameters

projection Func<ProjectionDefinitionBuilder<T>, ProjectionDefinition<T, TProjection>>

p => p.Include("Prop1").Exclude("Prop2")

Returns

UpdateAndGet<T, TProjection>

Project(Expression<Func<T, TProjection>>)

Specify how to project the results using a lambda expression

public UpdateAndGet<T, TProjection> Project(Expression<Func<T, TProjection>> expression)

Parameters

expression Expression<Func<T, TProjection>>

x => new Test { PropName = x.Prop }

Returns

UpdateAndGet<T, TProjection>

WithArrayFilter(Template)

Specify a single array filter using a Template to target nested entities for updates

public UpdateAndGet<T, TProjection> WithArrayFilter(Template template)

Parameters

template Template

Returns

UpdateAndGet<T, TProjection>

WithArrayFilter(string)

Specify an array filter to target nested entities for updates (use multiple times if needed).

public UpdateAndGet<T, TProjection> WithArrayFilter(string filter)

Parameters

filter string

{ 'x.SubProp': { $gte: 123 } }

Returns

UpdateAndGet<T, TProjection>

WithArrayFilters(Template)

Specify multiple array filters with a Template to target nested entities for updates.

public UpdateAndGet<T, TProjection> WithArrayFilters(Template template)

Parameters

template Template

The template with an array [...] of filters

Returns

UpdateAndGet<T, TProjection>

WithPipeline(Template)

Specify an update pipeline with multiple stages using a Template to modify the Entities.

NOTE: pipeline updates and regular updates cannot be used together.

public UpdateAndGet<T, TProjection> WithPipeline(Template template)

Parameters

template Template

A Template object containing multiple pipeline stages

Returns

UpdateAndGet<T, TProjection>

WithPipelineStage(Template)

Specify an update pipeline stage using a Template to modify the Entities (use multiple times if needed)

NOTE: pipeline updates and regular updates cannot be used together.

public UpdateAndGet<T, TProjection> WithPipelineStage(Template template)

Parameters

template Template

A Template object containing a pipeline stage

Returns

UpdateAndGet<T, TProjection>

WithPipelineStage(string)

Specify an update pipeline stage to modify the Entities (use multiple times if needed)

NOTE: pipeline updates and regular updates cannot be used together.

public UpdateAndGet<T, TProjection> WithPipelineStage(string stage)

Parameters

stage string

{ $set: { FullName: { $concat: ['$Name', ' ', '$Surname'] } } }

Returns

UpdateAndGet<T, TProjection>