1

I am looking at using LiteDb for C# as a document storage system.

I can upload and download so long as I know the ID of the file - but I want to have the option to search for it as well.

LiteDb documentation gives this command for searching all the files stored in a particular 'directory' within the Db;

 var files = db.FileStorage.Find("$/photos/2014/");

However I can't figure out what to do with the files variable. I can't convert it to a usable string etc.

I would want to add it to a listview for example.

Does anyone have any experience with using LiteDb and file searches?

Adrian Brown
  • 73
  • 1
  • 9

2 Answers2

4

A little late to answer, but it might still be useful. The better way to search through the files is to access the "_files" collection which will allow you to search for the file's metadata:

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
}

You can then use the "_files" class to access your file information to search for it. In this case it will retrieve all files with "somefile" in the filename:

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

You can then iterate through the results to access the file's information:

foreach(var searchResult in files)
{
    var fileInfo = $"Id: {searchResult.Id} | Filename: {searchResult.filename} | Length: {searchResult.length}";
    Console.WriteLine(fileInfo);
}

This will output something similar to:

Id: file1 | Filename: important.doc | Length: 73453
Id: file2 | Filename: less_important.doc | Length: 3476

Also, do not forget to create indexes for the properties you search a lot on. For example, in the above example I searched on the filename. I could create an index for the filename property as follows:

new LiteDatabase("myDatabase.db")
    .GetCollection<_files>()
    .EnsureIndex(files => files.filename); //Create an index for filename
Stitch10925
  • 172
  • 11
0

I managed to work out the answer....shared should it be useful to anyone else.

 listView1.Items.Clear();
        try
        {
    //"" is a blank search which will return all files. 
    //But could easily be a value from a textbox. It is loaded into the Array Files

            var files = db.FileStorage.Find("").ToArray();

    //go through each obj in the array and abstract FileName, ID and 
    //Created date, adding them each to a new array (mylist, mylist1 etc.)
foreach (object obj in files)
            {

                mylist = files.Select(I => Convert.ToString(I.Filename)).ToArray();
                mylist1 = files.Select(I => Convert.ToString(I.UploadDate)).ToArray();
                mylist2 = files.Select(I => Convert.ToString(I.Id)).ToArray();


            }

 //loop through the mylist arrays an create usable strings for each value.
            for (int i = 0; i < mylist.Length; i++)
            {
                name = mylist[i].ToString();
                datecreated = mylist1[i].ToString();
                id = mylist2[i].ToString();
 //add each value to a listview
                listView1.Items.Add(new ListViewItem(new string[] { name, datecreated,id }));

            }
Adrian Brown
  • 73
  • 1
  • 9