比较来自世界各地的卖家的域名和 IT 服务价格

Mongo多个 $lookup 和 $group 总共

所以我正在尝试 "join" 3 不同的集合 mongo 在查询中。 所以我需要的是有点
$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>
已邀请:

风见雨下

赞同来自:

谢谢艾滋病 @Veeram 我收到了一个小设置。


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: 'userId',
foreignField: 'users._id',
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: {
_id: "$_id",
name: "$name",
type: "$type",
user_id: "$users._id"
},
user_id: {
$first: "$users._id"
},
name: {
$first: "$users.name"
},
type: {
$first: "$users.type"
},
children: {
$push: "$users.lists"
}
}
},
{ $sort: { name: 1 } },
{
$group: {
_id: "$_id._id",
name: {
$first: "$_id.name"
},
type: {
$first: "$_id.type"
},
children: {
$push: {
_id: "$user_id",
name: "$name",
type: "$type",
children: "$children"
}
}
}
},
{ $sort: { name: 1 } }
]/;

知食

赞同来自:

未经测试,但我会尝试这样的东西:


db.groups.aggregate/
[
{ $match: { _parent: groupId } },
{
$lookup: {
from: 'users',
localField: '_id',
foreignField: 'group',
as: 'users'
}
},
{ $unwind: "$users" },
{ $lookup: {
from: 'lists',
localField: 'userId',
foreignField: 'users._id',
as: 'users.lists'
}
},
{ $group: {
_id: "$_id",
groupname: { $first: "$name" },
users: {
$push: "$users"
}
} }
]/

小明明

赞同来自:

您必须使用信息
group

/
_id: {_id: "$_id", name: "$name", type: "$type", user_id: "$users._id" }

/ 在
$group

在评论之后。

有点


{
$group: {
_id: {
_id: "$_id",
name: "$name",
type: "$type",
user_id: "$users._id"
},
name: {
$first: "$users.name"
},
type: {
$first: "$users.type"
},
children: {
$push: "$users.lists"
}
}
}, {
$sort: {
name: 1
}
}, {
$group: {
_id: "$_id._id",
name: {
$first: "$_id.name"
},
type: {
$first: "$_id.type"
},
users: {
$push: {
_id:"$user_id",
name: "$name",
type: "$type",
children: "$children"
}
}
}
}

要回复问题请先登录注册