Litedb: How do I migrate data structures ?

Created on 10 Mar 2017  路  2Comments  路  Source: mbdavid/LiteDB

I LOVE THIS DATABASE!

If I have an existing database and change the structure how do I migrate the existing data in the file or is it done automagically ?

Is there a file viewer available ?
(If I want to copy a data file off an iOS or Android device and view the contents)

Thanks for this awesome project !!!!!

question

Most helpful comment

Hi @stephenhauck, thanks for your comment. :)

About upgrade your datastructre, it麓s depend what you change. If you only add new field, no problems. If you remove a field, all data will keep this field, but when read/write this extra field will be removed.

The major problem is if you what change datatype in an existing data. In this case, you must "update" your collection to this. You can use UserVersion to control your datafile version schema. Also, you can use BsonDocument to access your old data and update. Some like this:

using(var db = new LiteDatabase("..."))
{
    if(db.Engine.UserVersion == 0)
    {
        foreach(var doc in db.Engine.Find("MyCol"))
        {
            doc["NewCol"] = Convert.ToInt32(doc["OldCol"].AsString);
            db.Engine.Update("MyCol", doc);
        }
        db.Engine.UserVersion = 1;
    }
    ....
}

You can use LiteDB Viewer: https://github.com/falahati/LiteDBViewer

All 2 comments

Hi @stephenhauck, thanks for your comment. :)

About upgrade your datastructre, it麓s depend what you change. If you only add new field, no problems. If you remove a field, all data will keep this field, but when read/write this extra field will be removed.

The major problem is if you what change datatype in an existing data. In this case, you must "update" your collection to this. You can use UserVersion to control your datafile version schema. Also, you can use BsonDocument to access your old data and update. Some like this:

using(var db = new LiteDatabase("..."))
{
    if(db.Engine.UserVersion == 0)
    {
        foreach(var doc in db.Engine.Find("MyCol"))
        {
            doc["NewCol"] = Convert.ToInt32(doc["OldCol"].AsString);
            db.Engine.Update("MyCol", doc);
        }
        db.Engine.UserVersion = 1;
    }
    ....
}

You can use LiteDB Viewer: https://github.com/falahati/LiteDBViewer

Awesome, THANKS for the quick response...hopefully this will help others :)

Was this page helpful?
0 / 5 - 0 ratings