У меня две коллекции.
Первый идентификатор стручки , имеющие следующую структуру:
{
"_id": "5ae46c0aff1bde1634ad8b3f",
"admin": "595bdd5aa6ea2811d48a7802",
"name": "Fashion Trend",
"datetime": 1524919038771,
"approval": false,
"actions": "Comments",
"description": "Fashion is about dressing according to what's fashionable. Style is more about being yourself.",
"profilepic": "https://cdn.com/upload/Community_photos/595bdd5aa6ea2811d48a7802_1524919188491.jpg",
"coverpic": "https://cdn.com/upload/Community_photos/595bdd5aa6ea2811d48a7802_1524919171374.jpg",
"__v": 0
}
секунда - pod_members , а структура выглядит следующим образом:
{
"_id": "5b127683187a4f19901e34b7",
"__v": 0,
"pod_id": "5ae470d04e15d607f043a20e",
"instaid": "5ad9a44cabd2180d64073462",
"username": "tagtor",
"user_id": "595bdd5aa6ea2811d48a7802",
"member_name": "Sagar Dalal",
"req_date": 1527936643123,
"datetime": 1527938142745,
"comments": ["Love the page. ? ? ? keep up the good job.", "Great job done with this page.?", "Fabulous. ? ? ??", "Keep up the great job. Well done.?? ? ?", "Amazing instagram page. ? ??"]
}
Мне нужно total_members для каждого pod также, если доза pod не имеет результата для участника, должна возвращаться 0 от имени.
Мой запрос похож на:
Pod.aggregate([
{
$lookup: {
from: "pod_members",
localField: "_id",
foreignField: "pod_id",
as: "meminfo"
}
},
{ $unwind: "$meminfo" },
{
$group: {
_id: "meminfo.pod_id",
total_members: { $sum: 1 }
}
},
{ $addFields: { "Status": false } }
]).exec(function (err, resp) {
if (err) console.log(err)
else console.log(resp)
})
Но это не дает мне желаемого результата.
Мне нужен результат Как:
[{ _id: 5ae46c0aff1bde1634ad8b3f,
admin: 595bdd5aa6ea2811d48a7802,
name: 'Fashion Trend',
datetime: 1524919038771,
approval: false,
actions: 'Comments',
description: 'Fashion is about dressing according to what\'s fashionable. Style is more about being yourself.',
profilepic: 'https://cdn.com/upload/Community_photos/595bdd5aa6ea2811d48a7802_1524919188491.jpg',
coverpic: 'https://cdn.com/upload/Community_photos/595bdd5aa6ea2811d48a7802_1524919171374.jpg',
__v: 0,
total_members: 1,
Status: false }]