【问题标题】:FindAll in MongoDB .NET Driver 2.0MongoDB .NET 驱动程序 2.0 中的 FindAll
【发布时间】:2015-06-14 12:21:26
【问题描述】:

我想使用 MongoDB .NET Driver 2.0 查询我的 MongoDB 集合而不使用任何过滤器,但我没有找到方法。我有以下解决方法,但看起来很奇怪:D

var filter = Builders<FooBar>.Filter.Exists(x => x.Id);
var fooBars = await _fooBarCollection.Find(filter)
    .Skip(0)
    .Limit(100)
    .ToListAsync();

有没有办法在 MongoDB .NET Driver 2.0 中发出不带过滤器的查询?

【问题讨论】:

  • 使用 empty BSON 文档怎么样。这是基本的 shell 要求。
  • 你能分享一个示例 sn-p 吗?我不知道你到底是什么意思:s
  • 大量的例子在左右移动。你有没有试过在没有“过滤器”的情况下提交你的.Find()?这对我们大多数人来说意味着全部。否则只是:new BsonDocument()
  • “你有没有试过在没有“过滤器”的情况下提交你的 .Find()?没有不带参数的 Find 重载。

标签: c# .net mongodb mongodb-.net-driver mongodb-csharp-2.0


【解决方案1】:

你不能在没有过滤器的情况下使用Find

但是,您可以使用通过所有内容的过滤器:

var findFluent = await _fooBarCollection.Find(_ => true);

或者您可以使用等效的空文档:

var findFluent = await _fooBarCollection.Find(new BsonDocument());

他们还添加了一个空过滤器,但它仅适用于较新版本的驱动程序:

var findFluent = await _fooBarCollection.Find(Builders<FooBar>.Filter.Empty);

【讨论】:

    【解决方案2】:

    FindAll() 是 MongoDB 1x 系列驱动程序的一部分。要在 2x 系列驱动程序中获得相同的功能,请使用带有空 BSON 文档的 find()。

    var list = await collection.Find(new BsonDocument()).ToListAsync();
    foreach (var dox in list)
    {
        Console.WriteLine(dox);
    }
    

    Reference

    【讨论】:

      【解决方案3】:

      对我有用

      public class TestProductContext
      {
          MongoClient _client;
          IMongoDatabase _db;
      
          public TestProductContext()
          {
              _client = new MongoClient("mongodb://localhost:27017");
              _db = _client.GetDatabase("EmployeeDB");
      
          }
      
          public IMongoCollection<Product> Products => _db.GetCollection<Product>("Products");
      }
      
      public class DataAccess
      {
          private TestProductContext _testProductContext;
      
          public DataAccess(TestProductContext testProductContext)
          {
              _testProductContext = testProductContext;
          }
          public List<Product> GetProducts()
          {
              List<Product> pto = new List<Product>();
              var cursor = _testProductContext.Products.Find(new BsonDocument()).ToCursor();
              foreach (var document in cursor.ToEnumerable())
              {
                  pto.Add(document);
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        要匹配所有元素,您可以使用FilterDefinitionBuilder.Empty 属性。当用户可以选择查询整个集合或按单个属性过滤时,我会使用此属性。

        如果您只想查询整个集合,而没有过滤器的可能性,那么这不是必需的。

        var builder = Builders<Object>.Filter;
                         matchFilter;
                        
        FilterDefinition<Object> matchFilter = builder.Empty;
           
        var results = await collection.Aggregate()
                                      .Match(filter)
                                      .ToListAsync()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-13
          • 2011-11-19
          相关资源
          最近更新 更多