Diana Barros
Diana Barros

Reputation: 57

How to apply soft delete filter in MongoDB Driver for C#?

public class User
{
        public string Id { get; private set; }
        public string Name { get; private set;} 
        public bool IsActive{ get; private set; }
}

I am building a .Net application using MongoDB Driver to connect to Mongo. Some of the implemented entities have an IsActive field - basically a boolean which is set to true if the entity is active and false if the entity has been deleted. (A soft or logical delete if you will)

My aim is to apply a "global filter" that would be the base of any filter applied to the given collection. Something like EF Core global query filters https://learn.microsoft.com/en-us/ef/core/querying/filters

Is there anything like this for MongoDB Driver? If not, do any of you have suggestions on how to implement this in any other way?

Upvotes: 5

Views: 795

Answers (1)

dododo
dododo

Reputation: 4897

The only option available out of the box is IMongoCollection.OfType, where filtering happens by an entity type (not fields), so you will need to change your implementation a bit to use it.

Upvotes: 0

Related Questions