回答有点晚了,但它可能仍然有用。搜索文件的更好方法是访问“_files”集合,这将允许您搜索文件的元数据:
class _files
{
public string Id { get; set; }
public string filename { get; set; }
public string mimeType { get; set; }
public long length { get; set; }
public int chunks{ get; set; }
public DateTime uploadDate { get; set; }
public object metadata { get; set; } //You could replace object with a custom class
}
然后,您可以使用“_files”类访问您的文件信息以进行搜索。在这种情况下,它将检索文件名中带有“somefile”的所有文件:
var files = new LiteDatabase("myDatabase.db")
.GetCollection<_files>() //Accesses the _files collection where file info is stored
.Find(file => file.Filename.Contains("somefile")); //Find files with "somefile" in the filename
然后,您可以遍历结果以访问文件的信息:
foreach(var searchResult in files)
{
var fileInfo = $"Id: {searchResult.Id} | Filename: {searchResult.filename} | Length: {searchResult.length}";
Console.WriteLine(fileInfo);
}
这将输出类似于:
Id: file1 | Filename: important.doc | Length: 73453
Id: file2 | Filename: less_important.doc | Length: 3476
此外,不要忘记为您经常搜索的属性创建索引。例如,在上面的示例中,我搜索了文件名。我可以为文件名属性创建一个索引,如下所示:
new LiteDatabase("myDatabase.db")
.GetCollection<_files>()
.EnsureIndex(files => files.filename); //Create an index for filename