【发布时间】:2021-06-01 04:59:20
【问题描述】:
我有一个带有字典字段的数据库表,其中键是字符串,值是 int 列表。(Dictionary<string,List<int>>()) 但现在我已将该字典更改为 (Dictionary<string,list<string>>())。但数据库表中已有旧记录字典值(list<int>)。从表中获取记录时出现序列化错误。有任何方法可以将这些值从 int 更新为字符串。我的意思是 int 值列表将替换为字符串列表或更改将 int 值转换为字符串。
我可以用 updateMany() mongodb 函数做到这一点吗?
如果是,那将如何?
这是一个示例记录。 assignscopes 和 scopes 是一个字典
{
"_id" : "14390b07-9407-4abb-9504-34eb02d504c1",
"Name" : "Bhavin Varsur",
"UserName" : "b10",
"Email" : "bhavin@gmail.com",
"MobileNumber" : "1016345823",
"Applications" : [
{
"_id" : "19e1b485-3077-49ba-81a2-13dec1abc012",
"UserRoles" : [
{
"Name" : "Super Admin",
"Permissions" : null,
"AssignScopes" : {
"Branch" : [
0
],
"Unit" : [],
"Option" : []
},
"Scopes" : {
"Region" : [
3
],
"Branch" : [
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
"Unit" : [
1, 2, 3, 4, 5, 44, 6, 7, 8, 9, 10, 11, 52, 12, 13, 14, 15, 16, 17, 18
],
"Option" : [
1, 2, 3, 4, 5, 6, 7, 8, 9
]
}
}
]
}
这是将字典值更改为字符串后的模型
public class UserRole
{
public string Name { get; set; }
public List<string> Permissions { get; set; }
[BsonDictionaryOptions(DictionaryRepresentation.Document)]
public Dictionary<string, List<string>> AssignScopes { get; set; } = new Dictionary<string, List<string>>();
[BsonDictionaryOptions(DictionaryRepresentation.Document)]
public Dictionary<string, List<string>> Scopes { get; set; } = new Dictionary<string, List<string>>();
}
【问题讨论】:
标签: mongodb dictionary mongodb-query mongodb-.net-driver