Closed wilberolive closed 4 years ago
Hi @wilberolive,
Yes, this tips are still valid for v4... You can use LiteEngine class to access your data using BsonDocument (an schemaless data structure). Using UserVersion
you can control your database version.
There is no rename key, so you need add a new field (using old value) and then remove old field.
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); doc.Remove("OldCol"); db.Engine.Update("MyCol", doc); } db.Engine.UserVersion = 1; } .... }
To remove a field, just use Remove
method in BsonDocument and execute Update
.
There is no problem with field order. All you class data will be serialized WITH fields, as JSON do. So, order doesn't matter.
You can also run shell commands to update your data without write any code. It's ideal when you have a single database to update. You can run
db.MyCol.update NewCol = OldCol
db.MyCol.update Fullname = UPPER(Last) + ', ' + UPPER(First)
Shell command also supported using Run
method. Shell does not have all functions implementations and suggestion are welcome.
I will add more options in shell and then will write a wiki page
Thanks for all the info. Will try all this out. Unfortunately I cannot use the Shell and have to do it through code as the database will reside on individual client machines that I don't have direct access to once the app is deployed. So anytime the data changes, I need the app itself to manage those changes and keep its own database updated.
@wilberolive, you can use shell commands inside your code using Run
method in LiteEngine
. Sometimes are easy to modify you already exist data.
Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!
Hi @wilberolive,
- Yes, this tips are still valid for v4... You can use LiteEngine class to access your data using BsonDocument (an schemaless data structure). Using
UserVersion
you can control your database version.- There is no rename key, so you need add a new field (using old value) and then remove old field.
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); doc.Remove("OldCol"); db.Engine.Update("MyCol", doc); } db.Engine.UserVersion = 1; } .... }
- To remove a field, just use
Remove
method in BsonDocument and executeUpdate
.- There is no problem with field order. All you class data will be serialized WITH fields, as JSON do. So, order doesn't matter.
You can also run shell commands to update your data without write any code. It's ideal when you have a single database to update. You can run
db.MyCol.update NewCol = OldCol
db.MyCol.update Fullname = UPPER(Last) + ', ' + UPPER(First)
Shell command also supported using
Run
method. Shell does not have all functions implementations and suggestion are welcome.I will add more options in shell and then will write a wiki page
This answer does not fit into current version of database (nuget package v 5.0.17). Kindly provide new code sample. Thank you.
Hi I'm just wondering how I handle some simple migration scenarios when the data structure changes.
I found this, which talks about changing a data type of a field. However the code seems to be different now and requires some sort of query. Is this still a valid solution for changing a field's data type?
How do you handle changing the name of a field? So for example, I have a collection of items and the items class has a property called Price. However I would like to rename that property to Cost for example.
How do you remove a field? Say if I remove the Price property entirely from the items class. I notice that the column seems to stay in the database. Can I remove the column somehow?
I'm also wondering what happens if you change the order of properties within a collection's class. Does that matter at all?
I think you should add a page to the wiki that covers all of the common migration scenarios as it would be a common problem for any real world application.