I'm unable to properly recall nested objects in LiteDB. An object that contains a collection of children of the same object (i.e. a tree structure) fails to populate the collection when retrieved from the database.
Code
A simple object (MyObj.cs):
using LiteDB;
public class MyObj
{
public string Name { get; set; } = "";
public IEnumerable<MyObj> Children = new List<MyObj>();
public override string ToString()
{
var childStr = string.Join(", ",Children.Select(c => c.ToString()).ToArray());
return $"[ {Name}: {childStr} ]";
}
}
A basic console application to instantiate the object and test (Program.cs):
using LiteDB;
var myObj = new MyObj() {
Name = "root",
Children = new List<MyObj>() {
new MyObj() {
Name = "Level1",
Children = new List<MyObj>()
}
}
};
const string dbFile = "test.db";
const string collection = "MyObjs";
using (var db = new LiteDatabase(dbFile))
{
var col = db.GetCollection<MyObj>(collection);
col.Insert(myObj);
}
Console.WriteLine(myObj);
using (var db = new LiteDatabase(dbFile))
{
var col = db.GetCollection<MyObj>(collection);
var found = col.FindOne(m => m.Name == "root");
Console.WriteLine(found);
}
Output:
[ root: [ Level1: ] ]
[ root: ]
As can be seen, the second line shows that Level1 is not restored correctly. I'm new to LiteDB so assume I am missing something - should this be possible? Do I need to use BsonRef or similar to achieve this?
Question
I'm unable to properly recall nested objects in LiteDB. An object that contains a collection of children of the same object (i.e. a tree structure) fails to populate the collection when retrieved from the database.
Code
A simple object (MyObj.cs):
A basic console application to instantiate the object and test (Program.cs):
Output:
As can be seen, the second line shows that Level1 is not restored correctly. I'm new to LiteDB so assume I am missing something - should this be possible? Do I need to use BsonRef or similar to achieve this?
Tested with LiteDB v5.0.21, .Net v8.0.303
Many thanks for any pointers.