【问题标题】:Mongo DB - UpdateMany columns dataMongodb - 更新多列数据
【发布时间】: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


    【解决方案1】:

    您可以使用这样的嵌套映射函数运行管道更新命令:

    db.collection.updateMany({},
    [
      {
        $set: {
          Applications: {
            $map: {
              input: "$Applications",
              as: "a",
              in: {
                _id: "$$a._id",
                UserRoles: {
                  $map: {
                    input: "$$a.UserRoles",
                    as: "ur",
                    in: {
                      Name: "$$ur.Name",
                      Permissions: "$$ur.Permissions",
                      AssignScopes: {
                        Branch: {
                          $map: {
                            input: "$$ur.AssignScopes.Branch",
                            as: "b",
                            in: {
                              $toString: "$$b"
                            }
                          }
                        },
                        Unit: "$$ur.Unit", // do another map if these are also integers
                        Option: "$$ur.Option" // do another map if these are also integers                    
                      },
                      Scopes: {
                        Region: {
                          $map: {
                            input: "$$ur.Scopes.Region",
                            as: "r",
                            in: {
                              $toString: "$$r"
                            }
                          }
                        },
                        Branch: {
                          $map: {
                            input: "$$ur.Scopes.Branch",
                            as: "b",
                            in: {
                              $toString: "$$b"
                            }
                          }
                        },
                        Unit: {
                          $map: {
                            input: "$$ur.Scopes.Unit",
                            as: "u",
                            in: {
                              $toString: "$$u"
                            }
                          }
                        },
                        Option: {
                          $map: {
                            input: "$$ur.Scopes.Option",
                            as: "o",
                            in: {
                              $toString: "$$o"
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    ])
    

    https://mongoplayground.net/p/nYqmfBFftPN

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-30
      • 2023-03-22
      • 2018-11-19
      • 1970-01-01
      • 2021-11-23
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多