Skip to content

Instantly share code, notes, and snippets.

@stormbreakers
Created December 13, 2019 12:58
Show Gist options
  • Save stormbreakers/d326894f5af314b93bc4b6bac693d085 to your computer and use it in GitHub Desktop.
Save stormbreakers/d326894f5af314b93bc4b6bac693d085 to your computer and use it in GitHub Desktop.
db.cohorts.aggregate([
{
"$match": { "journeyId": "4f1f0071fbe1093a8281" }
},
{
$project: {
_id: {
"$toString": "$_id"
},
testCohort: 1,
enabled: 1,
suppressAnalytics: 1,
excludeFromDashboard: 1,
title: 1,
startDateTime: 1,
customerId: 1,
journeyId: 1,
}
},
{
$lookup: {
from: "users",
localField: "_id",
foreignField: "journeys.cohortId",
as: "userList"
}
},
{
$project: {
_id: 1,
testCohort: 1,
enabled: 1,
suppressAnalytics: 1,
excludeFromDashboard: 1,
title: 1,
startDateTime: 1,
customerId: 1,
journeyId: 1,
userCount: { $size: "$userList" }
}
}
])
db.getCollection('cohorts').aggregate([
{ "$match": { "journeyId": "4f1f0071fbe1093a8281" } }
,
{
$lookup: {
from: "users",
let: { cohort_id: "$_id" },
pipeline: [
{ $match: { "journeys.cohortId": "$$cohort_id" } },
{ $project: { _id: 0, root: { fname: "$fname", lname: "$lname" } } },
{ $replaceRoot: { newRoot: "$root" } }
],
as: "userList"
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment