在MongoDB .NET Driver 2.0中查找所有

tug*_*erk 13 .net c# mongodb mongodb-csharp-2.0 mongodb-.net-driver

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

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

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

i3a*_*non 22

Find没有过滤器就无法使用.

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

var findFluent = await _fooBarCollection.Find(_ => true);
Run Code Online (Sandbox Code Playgroud)

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

var findFluent = await _fooBarCollection.Find(new BsonDocument());
Run Code Online (Sandbox Code Playgroud)

他们还添加了一个空过滤器,但它只能在较新版本的驱动程序中使用:

var findFluent = await _fooBarCollection.Find(Builders<FooBar>.Filter.Empty);
Run Code Online (Sandbox Code Playgroud)