I have the following set of documents,
[
{
_id: new ObjectId("62f695063cf6055e6a50fd6d"),
permissionGroupName: "User Management",
permissionName: "Create User",
__v: 0,
},
{
_id: new ObjectId("62f695063cf6055e6a50fd6f"),
permissionGroupName: "User Management",
permissionName: "Edit User",
__v: 0,
},
{
_id: new ObjectId("62f695063cf6055e6a50fd71"),
permissionGroupName: "User Management",
permissionName: "Delete User",
__v: 0,
},
{
_id: new ObjectId("62f695073cf6055e6a50fd73"),
permissionGroupName: "User Management",
permissionName: "Approve User",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Country Management",
permissionName: "Approve Country",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Advertisement Management",
permissionName: "Create Advertisement",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Advertisement Management",
permissionName: "Delete Advertisement",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "University Management",
permissionName: "Create University",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "University Management",
permissionName: "Delete University",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "University Management",
permissionName: "Approve University",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Role Management",
permissionName: "Approve Role",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Role Management",
permissionName: "Delete Role",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Scholorship Management",
permissionName: "Approve Scholorship",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Post Management",
permissionName: "Approve Post",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Post Management",
permissionName: "Delete Post",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Student Management",
permissionName: "Create Student",
__v: 0,
},
{
_id: new ObjectId("62f8a73c79a839650a183182"),
permissionGroupName: "Teacher Management",
permissionName: "Create Teacher",
__v: 0,
},
];
What I am trying to achieve
I am trying to transform it to the following form,
[
[
{
permissions: [
{
name: "Create User",
id: new ObjectId("62f695063cf6055e6a50fd6d"),
},
{
name: "Edit User",
id: new ObjectId("62f695063cf6055e6a50fd6f"),
},
{
name: "Delete User",
id: new ObjectId("62f695063cf6055e6a50fd71"),
},
{
name: "Approve User",
id: new ObjectId("62f695073cf6055e6a50fd73"),
},
],
permissionGroupName: "User Management",
},
{
permissions: [
{
name: "Approve Country",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Country Management",
},
{
permissions: [
{
name: "Create Advertisement",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete Advertisement",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Advertisement Management",
},
{
permissions: [
{
name: "Create University",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete University",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Approve University",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "University Management",
},
],
[
{
permissions: [
{
name: "Approve Role",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete Role",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Role Management",
},
{
permissions: [
{
name: "Approve Scholorship",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Scholorship Management",
},
{
permissions: [
{
name: "Approve Post",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete Post",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Post Management",
},
{
permissions: [
{
name: "Delete Student",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Student Management",
},
],
[
{
permissions: [
{
name: "Create Teacher",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Teacher Management",
},
],
];
As you can see the permissions are grouped by their group name and they have been set into arrays of objects, where each of the array containing four groups,
For example it has the following format,
[
[
{} <---- (1),
{} <---- (2),
{} <---- (3),
{} <---- (4)
],
[
{} <---- (1),
{} <---- (2),
{} <---- (3),
{} <---- (4)
],
[
{} <---- (1)
]
]
See they are grouped into arrays of four.
What I currently have
I have the following code,
[
{
$group: {
_id: "$permissionGroupName",
permissions: {
$push: {
name: "$permissionName",
id: "$_id",
},
},
},
},
{
$project: {
permissionGroupName: "$_id",
permissions: 1,
_id: 0,
},
},
]
Which gives me,
[
{
permissions: [
{
name: "Create User",
id: new ObjectId("62f695063cf6055e6a50fd6d"),
},
{
name: "Edit User",
id: new ObjectId("62f695063cf6055e6a50fd6f"),
},
{
name: "Delete User",
id: new ObjectId("62f695063cf6055e6a50fd71"),
},
{
name: "Approve User",
id: new ObjectId("62f695073cf6055e6a50fd73"),
},
],
permissionGroupName: "User Management",
},
{
permissions: [
{
name: "Approve Country",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Country Management",
},
{
permissions: [
{
name: "Create Advertisement",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete Advertisement",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Advertisement Management",
},
{
permissions: [
{
name: "Create University",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete University",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Approve University",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "University Management",
},
{
permissions: [
{
name: "Approve Role",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete Role",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Role Management",
},
{
permissions: [
{
name: "Approve Scholorship",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Scholorship Management",
},
{
permissions: [
{
name: "Approve Post",
id: new ObjectId("62f8a73c79a839650a183182"),
},
{
name: "Delete Post",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Post Management",
},
{
permissions: [
{
name: "Delete Student",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Student Management",
},
{
permissions: [
{
name: "Create Teacher",
id: new ObjectId("62f8a73c79a839650a183182"),
},
],
permissionGroupName: "Teacher Management",
},
];
It gives me all the objects without grouping them into arrays of four. Can someone please tell me how to proceed from here to get them into grouped into arrays of four?
CodePudding user response:
What you ask for is not a valid mongoDB response, as mongoDB aggregation pipeline returns an array of documents (objects). But you can get close to it.
Since mongoDB version 5.0 you can use $setWindowFields
for this and add to your current query 3 more steps:
{$setWindowFields: {
sortBy: {nothing: 1},
output: {
index: { $sum: 1,
window: {documents: ["unbounded", "current"]}}
}
}
},
{$group: {
_id: {$ceil: {$divide: ["$index", 4]}},
data: {
$push: {
permissions: "$permissions",
permissionGroupName: "$permissionGroupName"
}
}
}},
{$unset: "_id"}
See how it works on the playground example