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] Does LiteDB cope with nested objects of the same type? #2540

Open
malisimat opened this issue Sep 6, 2024 · 0 comments
Open

[QUESTION] Does LiteDB cope with nested objects of the same type? #2540

malisimat opened this issue Sep 6, 2024 · 0 comments
Labels

Comments

@malisimat
Copy link

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):

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?

Tested with LiteDB v5.0.21, .Net v8.0.303

Many thanks for any pointers.

@malisimat malisimat added the bug label Sep 6, 2024
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

1 participant