在Go语言中使用MongoDB将“用户与角色”转换为“角色与用户”。

huangapple go评论93阅读模式
英文:

Transposing "user with role" to "role with user" in go with mongodb

问题

我正在使用Go语言和MongoDB驱动程序开发一个应用程序。我有一个用户集合,其中包含分配的角色。为了方便起见,我将角色存储在一个CSV字符串中。例如:

bill.roles = "user,billing,admin"
bob.roles = "user"
sandy.roles = "user,billing"

我需要将它们转换为角色到用户的映射。例如:

user = "bill,bob,sandy"
billing = "bill,sandy"
admin = "bill"

目前,我通过循环遍历集合中的用户切片,并构建一个map[string][]string,然后将其连接回CSV字符串来实现这一点。

将CSV与切片的讨论放在一边(我很乐意)...有没有更好的方法让MongoDB在数据库中执行转换,而不是进行所有这些循环?或者我已经在正确的轨道上了吗?

英文:

I am working on an app in Go with the MongoDB driver. I have a collection of users with roles assigned. I'm storing the roles in a csv string just for convenience. e.g.

bill.roles = "user,billing,admin"
bob.roles = "user"
sandy.roles = "user,billing"

I need to transpose them into a role to user mapping. e.g.

user = "bill,bob,sandy"
billing = "bill,sandy"
admin = "bill"

I'm currently doing it by looping through the users slice from the collection and building a map[string][]string that I join back to a csv string

Putting the csv vs slice discussion aside (I am open)... Is there a better way to get mongodb to do the transformation in the database rather than all this looping? Or am I already on the right track.

答案1

得分: 3

以下是存储用户角色并查询数据的一些细节 - 基于之前的评论等。考虑以下文档的集合 users,其中用户的角色存储在一个 数组 字段中:

{ name: "bob", roles: [ "admin", "user", "billing" ] }
{ name: "jane", roles: [ "user", "billing" ] }
{ name: "krish", roles: [ "admin", "user" ] }
{ name: "jill", roles: [ "user" ] }

查询 数据的方法如下(在 mongo shell 中运行查询):

db.users.find( { roles: "admin" } )    // 查找具有 admin 角色的用户
db.users.find( { roles: { $in: [ "admin", "billing" ] } } )    // 具有 admin 或 billing 角色的用户
db.users.find( { roles: { $all: [ "user", "billing" ] } } )    // 具有 user 和 billing 角色的用户
db.users.find( { roles: "admin" }, { _id: 0, name: 1 } )       // 具有 admin 角色的用户,只返回用户名

以下是一个聚合查询示例 - 按角色分组用户。您还可以进一步按角色、计数等进行排序(此处未显示):

db.users.aggregate([
  { 
      $unwind: "$roles" 
  },
  { 
      $group: { 
          _id: { role: "$roles" }, 
          users: { $push: "$name" } 
      } 
  }
])

输出结果:

{ "_id" : { "role" : "billing" }, "users" : [ "bob", "jane" ] }
{ "_id" : { "role" : "admin" }, "users" : [ "bob", "krish" ] }
{ "_id" : { "role" : "user" }, "users" : [ "bob", "jane", "krish", "jill" ] }

<br>

参考查询数组

英文:

Here are some details to store user roles and query the data - based upon earlier comments, etc. Consider the collection users with following documents - users with their roles stored in an array field:

{ name: &quot;bob&quot;, roles: [ &quot;admin&quot;, &quot;user&quot;, &quot;billing&quot; ] }
{ name: &quot;jane&quot;, roles: [ &quot;user&quot;, &quot;billing&quot; ] }
{ name: &quot;krish&quot;, roles: [ &quot;admin&quot;, &quot;user&quot; ] }
{ name: &quot;jill&quot;, roles: [ &quot;user&quot; ] }

Query the data as follows (the queries runs in mongo shell):

db.users.find( { roles: &quot;admin&quot; } )    // find users with admin role
db.users.find( { roles: { $in: [ &quot;admin&quot;, &quot;billing&quot; ] } } )    // users with admin or billing role
db.users.find( { roles: { $all: [ &quot;user&quot;, &quot;billing&quot; ] } } )    // users with user and billing roles
db.users.find( { roles: &quot;admin&quot; }, { _id: 0, name: 1 } )       // users with admin role, project only the user name

Here is an aggregation query - group users by role. You can also further sort by role, count, etc. (not shown here):

db.users.aggregate([
  { 
      $unwind: &quot;$roles&quot; 
  },
  { 
      $group: { 
          _id: { role: &quot;$roles&quot; }, 
          users: { $push: &quot;$name&quot; } 
      } 
  }
])

The output:

{ &quot;_id&quot; : { &quot;role&quot; : &quot;billing&quot; }, &quot;users&quot; : [ &quot;bob&quot;, &quot;jane&quot; ] }
{ &quot;_id&quot; : { &quot;role&quot; : &quot;admin&quot; }, &quot;users&quot; : [ &quot;bob&quot;, &quot;krish&quot; ] }
{ &quot;_id&quot; : { &quot;role&quot; : &quot;user&quot; }, &quot;users&quot; : [ &quot;bob&quot;, &quot;jane&quot;, &quot;krish&quot;, &quot;jill&quot; ] }

<br>

Reference: Query an Array

答案2

得分: 0

如果你在SQL中进行操作,只需使用GROUP BY进行聚合。

在MongoDB中:

db.accesslist.aggregate([
   { $group : { privilege : "$privilege", username: { $push: "$username" } } }
 ])

参考:

英文:

If you do it on SQL, just do GROUP BY aggregation.

In MongoDB :

db.accesslist.aggregate([
   { $group : { privilege : &quot;$privilege&quot;, username: { $push: &quot;$username&quot; } } }
 ])

Reference:

huangapple
  • 本文由 发表于 2021年6月26日 01:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/68134824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定