英文:
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: "bob", roles: [ "admin", "user", "billing" ] }
{ name: "jane", roles: [ "user", "billing" ] }
{ name: "krish", roles: [ "admin", "user" ] }
{ name: "jill", roles: [ "user" ] }
Query the data as follows (the queries runs in mongo
shell):
db.users.find( { roles: "admin" } ) // find users with admin role
db.users.find( { roles: { $in: [ "admin", "billing" ] } } ) // users with admin or billing role
db.users.find( { roles: { $all: [ "user", "billing" ] } } ) // users with user and billing roles
db.users.find( { roles: "admin" }, { _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: "$roles"
},
{
$group: {
_id: { role: "$roles" },
users: { $push: "$name" }
}
}
])
The output:
{ "_id" : { "role" : "billing" }, "users" : [ "bob", "jane" ] }
{ "_id" : { "role" : "admin" }, "users" : [ "bob", "krish" ] }
{ "_id" : { "role" : "user" }, "users" : [ "bob", "jane", "krish", "jill" ] }
<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 : "$privilege", username: { $push: "$username" } } }
])
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论