我正在尝试首先在group.name上对'group.name'填充“角色”文档,然后在role.name
上return this.find(query, { _group: 1, name: 1, _id:0 } )
.populate('_group', 'name -_id')
.sort({ '_group.name': 1, name: 1 })
但它没有按预期工作,我只在role.name
上排序ROLES:[
{"name":"NewRole"},
{"name":"NewRoleInGroup","_group":{"name":"GroupA"}},
{"_group":{"name":"GroupA"},"name":"admin"},
{"_group":{"name":"GroupA"},"name":"employee"},
{"_group":{"name":"GroupB"},"name":"employee"},
{"_group":{"name":"GroupC"},"name":"employee"},
{"_group":{"name":"GroupA"},"name":"manager"},
{"_group":{"name":"GroupB"},"name":"manager"},
{"name":"visitor"}
]
我想宁愿:
ROLES:[
// NO GROUP
{"name":"NewRole"},
{"name":"visitor"}.
// GroupA
{"_group":{"name":"GroupA"},"name":"admin"},
{"_group":{"name":"GroupA"},"name":"employee"},
{"_group":{"name":"GroupA"},"name":"manager"},
{"name":"NewRoleInGroup","_group":{"name":"GroupA"}},
// GroupB
{"_group":{"name":"GroupB"},"name":"employee"},
{"_group":{"name":"GroupB"},"name":"manager"},
// GroupC
{"_group":{"name":"GroupC"},"name":"employee"}
]
感谢您的反馈
UPDATE 根据@Talha评论:
使用聚合,我接近我需要的东西...但是,我得到一个数组内的组数据,在多对一关系中,在这种情况下只有一个组...如何投影group作为数组中的第一个对象:“group”:{}或“group”:{“name”; ......,“描述”:......}?
return this.aggregate([
{ $match: query},
{
$lookup: {
from: 'groups',
localField: '_group',
foreignField: '_id',
as: 'group'
}
}
,
{
$project: {
'group.name': 1,
'group.description': 1,
name: 1,
description: 1,
_id: 0
}
},
{ $sort: { 'name': 1 } },
{ $limit: limit },
{ $skip: skip }
])
ROLES: [
// NO GROUP
{"name":"NewRole","description":"NewRole description","group":[]},
{"name":"Visitor","description":"no permissions","group":[]},
// GROUP A
{"name":"NewRoleInGroup","description":"NewRoleInGroup description","group":[{"name":"GroupA","description":"Description GroupA"}]},
{"name":"admin","description":"can RW group, user","group":[{"name":"GroupA","description":"Description GroupA"}]},
{"name":"employee","description":"can R group, can read user","group":[{"name":"GroupA","description":"Description GroupA"}]},
{"name":"manager","description":"can R group, can RW user","group":[{"name":"GroupA","description":"Description GroupA"}]},
// GROUP B
{"name":"employee","description":"can R group, can read user","group":[{"name":"GroupB","description":"Description GroupB"}]},
{"name":"manager","description":"can R group, can RW user","group":[{"name":"GroupB","description":"Description GroupB"}]},
// GROUP C
{"name":"employee","description":"can R group, can read user","group":[{"name":"GroupC","description":"Description GroupC"}]}]
答案 0 :(得分:1)
我应该使用以下函数w aggregate:
return this.aggregate([
{ $match: query},
{
$lookup: {
from: 'groups',
localField: '_group',
foreignField: '_id',
as: 'group'
}
},
{
$project: {
group: {
$let:{
vars:{
firstGroup:{
$arrayElemAt:["$group",0]
}
},
in:{
name:"$$firstGroup.name",
description:"$$firstGroup.description"
}
}
},
name: 1,
description: 1,
_id: 0
}
},
{ $sort: { 'group.name': 1, 'name': 1 } },
{ $limit: limit },
{ $skip: skip }
])
.exec()
.then((list) => {
return list;
})
这将输出正确排序的列表(1 / by group.name 2 / ny role.name),并将该组输出到查找输出数组中:
ROLES: [
// NO GROUP
{"name":"NewRole","description":"NewRole description","group":{}},
{"name":"Visitor","description":"no permissions","group":{}},
// GROUP A
{"name":"NewRoleInGroup","description":"NewRoleInGroup description","group":{"name":"GroupA","description":"Description GroupA"}},
{"name":"admin","description":"can RW group, user","group":{"name":"GroupA","description":"Description GroupA"}},
{"name":"employee","description":"can R group, can read user","group":{"name":"GroupA","description":"Description GroupA"}},
{"name":"manager","description":"can R group, can RW user","group":{"name":"GroupA","description":"Description GroupA"}},
// GROUP B
{"name":"employee","description":"can R group, can read user","group":{"name":"GroupB","description":"Description GroupB"}},
{"name":"manager","description":"can R group, can RW user","group":{"name":"GroupB","description":"Description GroupB"}},
// GROUP C
{"name":"employee","description":"can R group, can read user","group":{"name":"GroupC","description":"Description GroupC"}}]