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

[QUESTION] How can I store an array of strings in this database? #1620

Closed
riksking opened this issue Apr 7, 2020 · 2 comments
Closed

[QUESTION] How can I store an array of strings in this database? #1620

riksking opened this issue Apr 7, 2020 · 2 comments
Labels

Comments

@riksking
Copy link

riksking commented Apr 7, 2020

I don`t wanna create new class to do this.
I need fast way to store and read array of strings and nothing else.

Is there a method to do this?
If not, are there plans to add?

@lbnascimento
Copy link
Contributor

@riksking I took the code you mentioned in #1597 and modified it:

public static void WriteToExistDb(IEnumerable<string> chunk, string pathToDb, string tableName)
{
	using (var testDb = new LiteDatabase(pathToDb))
	{
		var col = testDb.GetCollection(tableName);
		foreach (var str in chunk)
		{
			col.Insert(new BsonDocument { ["value"] = str });
		}
	}
}

public static IEnumerable<string> ReadFromExistDb(string pathToDb, sting tableName)
{
	using (var testDb = new LiteDatabase(pathToDb))
	{
		var col = testDb.GetCollection(tableName);
		foreach(var doc in col.FindAll())
		{
			yield return doc["value"].AsString;
		}
	}
}

@riksking
Copy link
Author

riksking commented Apr 8, 2020

@lbnascimento Thanks a lot.
It seems to me that it would be nice if it were possible to save primitive types without casting to a type BsonDocument. More precisely, that it should be implemented inside LiteDb.
As for example, this is implemented with an implicit identifier, which is created automatically if it is not in the specified type.

@riksking riksking closed this as completed Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants