c# - MongoDB C# 驱动程序 : Query interceptors?

标签 c# mongodb linq

MongoDB C# 驱动程序是否支持 Entity Framework 等查询拦截器?

我检查了文档,但找不到任何内容。

基本上,我需要做的是确保对数据库的某些查询(根据上下文)始终应用某些限制。

例如,如果我的文档可以被软删除,那么我总是需要确保为 { "SoftDeleted": false } 添加过滤器。 Entity Framework 通过查询拦截器优雅地处理这个问题。

最佳答案

MongoClient 允许订阅 CommandStartedEvent。以下是转储到控制台发送到服务器的每个命令的示例:

var mongoClient = new MongoClient(new MongoClientSettings
{
    Server = new MongoServerAddress("localhost", 27017),
    ClusterConfigurator = cb =>
    {
        cb.Subscribe<CommandStartedEvent>(e =>
        {
            Console.WriteLine($"{e.CommandName} - {e.Command.ToJson(new JsonWriterSettings { Indent = true })}");
            Console.WriteLine(new String('-', 32));
        });
    }
});

CommandStartedEvent 包含可用于特定逻辑的 CommandNameCommand 属性。

关于c# - MongoDB C# 驱动程序 : Query interceptors?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48947260/

相关文章:

c# - Mvc asp.net和n层架构

c# - 在 asp.net 中获取一行 # 和 proc 名称

ruby-on-rails - 我如何从 Ruby 代码连接到 mongodb?

mongodb - 从嵌套对象数组中删除对象 mongodb

c# - 如何为 Contains<T> 构建表达式树

c# - 使用页面方法访问页面时访问页面的方法

java - 使用 Java 展平 JSON 层次结构

c# - 当 a.Foo == b.Bar 时从 list<a> AND list<b> 中删除项目

linq - 如何将 DataTable 转换为 IEnumerable<Dictionary<string, object>>?

c# - HttpWebRequest 随着线程数的增加而变慢