C# 如何异步统计MongoDB GridFS bucket中的所有文件
我需要计算GridFS存储桶中的所有对象 我写了以下方法,但我想如果有更好的方法:C# 如何异步统计MongoDB GridFS bucket中的所有文件,c#,mongodb,gridfs,C#,Mongodb,Gridfs,我需要计算GridFS存储桶中的所有对象 我写了以下方法,但我想如果有更好的方法: public async Task<long> GetAllFilesCountAsync() { var bucket = new GridFSBucket(newDatabase, new GridFSBucketOptions { BucketName = "fs", ChunkSizeBytes = 1048576, // 1MB
public async Task<long> GetAllFilesCountAsync()
{
var bucket = new GridFSBucket(newDatabase, new GridFSBucketOptions
{
BucketName = "fs",
ChunkSizeBytes = 1048576, // 1MB
WriteConcern = WriteConcern.WMajority,
ReadPreference = ReadPreference.Secondary
});
var filter = Builders<GridFSFileInfo>.Filter.Empty;
var options = new GridFSFindOptions
{
};
using (var cursor = await bucket.FindAsync(filter, options))
{
var fileInfo = await cursor.ToListAsync(); // <-- this returns the whole collection
var count = fileInfo.Count();
return count;
}
}
公共异步任务GetAllFileCountAsync()
{
var bucket=new GridFSBucket(newDatabase,new GridFSBucket选项
{
BucketName=“fs”,
ChunkSizeBytes=1048576,//1MB
WriteConcern=WriteConcern.WMajority,
ReadPreference=ReadPreference.Secondary
});
var filter=Builders.filter.Empty;
var options=new GridFSFindOptions
{
};
使用(var cursor=wait bucket.FindAsync(过滤器,选项))
{
var fileInfo=wait cursor.ToListAsync()//