Closed bluekuen closed 2 years ago
Hi, you can use the LiteDB studio to update the type ref in your entries. If you have it already in Prod you can use the statements in a migration function.
Hello, thanks for the answer!
To clarify: I only save User in my database so i only have a User collection. These users store Devices in a list. Yes its already in prod and i already tried it this way:
var updateSQL = "UPDATE User SET _type = " + "REPLACE(_type, 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile', 'DeviceSet.Mobile.Models.BleDevice, DeviceSet') " + "WHERE _type LIKE 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile'";
_database.Execute(updateSQL);
But it didn't work somehow. Am i doing it wrong?
Thanks in advance!
ok, it looks like you try to set the _type of the user, not the the type of entry of the users device list. Can you paste a sample text output from LiteDB Studio?
There's just a zero:
I mean a user object
Ok i see. But how am i able to update the Devices inside the User? if i try update User.Devices it would be the list right? Also User.Devices can't be updated.
make a backup,
try something like:
UPDATE User SET $.Devices[@._type = 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile']._type = 'DeviceSet.Mobile.Models.BleDevice, DeviceSet'
thanks so much for your help! I tried the command you provided but get following error:
Unexpected token $
in position 17.
UPDATE User SET $.Devices[@._typ -----------------^
Do i need to escape with an escape char?
maybe omit?
UPDATE User SET Devices[@._type = 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile']._type = 'DeviceSet.Mobile.Models.BleDevice, DeviceSet'
hm, very tricky
maybe you can try convert it with a Untyped collection:
using(var db = new LiteDatabase("mydb.db"))
{
var col = db.GetCollection("User");
foreach (var user in col.FindAll())
{
foreach (var device in user["Devices"])
{
if (device["_type"] == "DeviceSetMobile.Models.BleDevice, DeviceSetMobile")
{
device["_type"] = "DeviceSet.Mobile.Models.BleDevice, DeviceSet";
}
}
col.Update(user);
}
}
Sadly didn't work either. I tried it this way:
UPDATE User SET Devices = Devices[*]._type = 'DeviceSet.Mobile.Models.BleDevice, DeviceSet'
But gave me this error:
Left expression MAP($.Devices[*]=>@._type)
returns more than one result. Try use ANY or ALL before operant.
Untyped collection also not working?
Untyped collection did work! Thank you very very much! You saved my day!
Discussed in https://github.com/mbdavid/LiteDB/discussions/2168