Mongo多个 $lookup 和 $group 总共
所以我正在尝试 "join" 3 不同的集合 mongo 在查询中。 所以我需要的是有点
和报告
在蒙古的聚合。
我的 3 收藏看起来像这样:
用户:
/伪码/
团体
:
列表:
/这是 "itemlists", 属于用户/:
所以我想做的是,给定一些组标识符或
/对于在最高级别的父母组的组/ - 获得一切
在这个小组里面,找到所有
在这些群体中,以及他们的
.
所以最后我需要这样的东西:
希望你明白我的意思!
我现在有什么 /感谢西蒙,赛道和许多研究/ - 请不要关注不可知论的语法 Meteor, 你了解了这个想法:
我希望有人能把我送到正确的方式 ... 最好
https://coderoad.ru/28387603/
, 我能找到哪个,并没有帮助我。
非常感谢,最好,p
</some></another></some>
$lookup
和报告
$group
在蒙古的聚合。
我的 3 收藏看起来像这样:
用户:
/伪码/
{
_id,
username: String,
group: <some group._id="">
}
团体
:
{
_id,
name: String,
_parent: <another group._id="">,
}
列表:
/这是 "itemlists", 属于用户/:
{
_id,
name: String,
userId: <some user._id="">
}
所以我想做的是,给定一些组标识符或
null
/对于在最高级别的父母组的组/ - 获得一切
groups
在这个小组里面,找到所有
users
在这些群体中,以及他们的
lists
.
所以最后我需要这样的东西:
[
{
_id: someGroupId,
name: someGroupName,
type: "group",
users: [
{
_id: someUserId,
name: someUserName,
type: "user",
lists: [
... and the same again for the lists /type: "list"/
]
},
... another user
]
},
... another group
]
希望你明白我的意思!
我现在有什么 /感谢西蒙,赛道和许多研究/ - 请不要关注不可知论的语法 Meteor, 你了解了这个想法:
Groups.aggregate/[
{ $match: { _parent: groupId } },
{ $sort: { name: 1 } },
{
$lookup: {
from: 'users',
localField: '_id',
foreignField: 'group',
as: 'users'
}
},
{
$unwind: {
path: "$users",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: 'lists',
localField: 'users._id',
foreignField: 'userId',
as: 'users.lists'
}
},
{
$unwind: {
path: "$users.lists",
preserveNullAndEmptyArrays: true
}
},
{
$match: {
$or: [
{ "users.lists": { $exists: false } },
{ "users.lists.supplier_id": supplierId }
]
}
},
{ $sort: { "users.lists.name": 1 } },
{
$project: {
"name": 1,
"type": { $literal: 'group' },
"users._id": 1,
"users.name": { $ifNull: ["$users.profile.company.name", "$users.username"] },
"users.type": { $literal: 'user' },
"users.lists._id": 1,
"users.lists.name": 1,
"users.lists.item_count": 1,
"users.lists.type": { $literal: 'list' }
}
},
{
$group: {
_id: '$users._id',
name: { $first: "$users.name" },
type: { $first: "$users.type" },
children: {
$push: "$users.lists"
}
}
},
{ $sort: { "users.name": 1 } },
// until here I have one document per user, with their lists inside the "children" key - all good!
// now I have to group the users inside their groups ...
// NOT WORKING: returns completely wrong stuff
{ $group: {
_id: '$_id',
name: { $first: "$name" },
type: { $first: "$type" },
children: {
$push: "$users"
}
} },
{ $sort: { name: 1 } }
]/;
我希望有人能把我送到正确的方式 ... 最好
https://coderoad.ru/28387603/
, 我能找到哪个,并没有帮助我。
非常感谢,最好,p
</some></another></some>
没有找到相关结果
已邀请:
3 个回复
风见雨下
赞同来自:
知食
赞同来自:
小明明
赞同来自:
/
/ 在
在评论之后。
有点