Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] LiteDB Docs FileStorage section "fs.Find("$/photos/2014/");" not supported in v5 #2506

Closed
lanwah opened this issue Jun 11, 2024 · 1 comment · May be fixed by #2567
Closed

[BUG] LiteDB Docs FileStorage section "fs.Find("$/photos/2014/");" not supported in v5 #2506

lanwah opened this issue Jun 11, 2024 · 1 comment · May be fixed by #2567
Labels

Comments

@lanwah
Copy link

lanwah commented Jun 11, 2024

Version
LiteDB version 5.0.20.0

Describe the bug
LiteDb Docs FileStroage section.

// Find all files references in a "directory"
var files = fs.Find("$/photos/2014/");

Not supported writing in v5.
It is recommended to add different versions of writing instructions for the Find method

Expected behavior
Supported writing for the Find method in different versions.
we can write like this in v5:

fs.Find("_id LIKE @0", path + "%")

or

fs.Find("_id = @0", path);

or

fs.Find("filename = @0", filename)

Screenshots/Stacktrace
image

@lanwah lanwah added the bug label Jun 11, 2024
@lanwah lanwah changed the title [BUG] [BUG] LiteDB Docs FileStorage section "fs.Find("$/photos/2014/");" not supported in v5 Jun 11, 2024
@Joy-less
Copy link
Collaborator

After some digging around, this is how you can do it:

// Open database connection
using LiteDatabase dataBase = new("demo.db");

// Get the file metadata/chunks storage
ILiteStorage<string> fileStorage = dataBase.GetStorage<string>("myFiles", "myChunks");

// Upload empty test file to file storage
using MemoryStream emptyStream = new();
fileStorage.Upload("$/photos/2014/picture-01.jpg", "picture-01.jpg", emptyStream);

// Find photos from 2014
var files = fileStorage.Find("_id LIKE '$/photos/2014/%'"); // returns 1 LiteFileInfo

The LIKE statement uses the SQL syntax where % is the wildcard character.

If you would like to use parameters, you can use this:

// Use parameters to avoid SQL injection
var files = fileStorage.Find("_id LIKE @0", "$/photos/2014/%");

If you would like to concisely find by the full ID, you can use this:

// Find by exact ID
var file = fileStorage.FindById("$/photos/2014/picture-01.jpg");

This should be updated in the docs; I can confirm what's written there is incorrect.

Joy-less added a commit to Joy-less/LiteDB that referenced this issue Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants