Last active
October 17, 2023 15:32
-
-
Save nevehallon/655e5d840f79c552a12e534f262e6627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AggregationOperatorsArg } from "@bageldb/bagel-db/dist/interfaces" | |
import { db } from "../bageldb" | |
// NOTE: currently only works with following version ! "@bageldb/bagel-db": "^0.4.30-beta.0.03", | |
const aggSteps: AggregationOperatorsArg = [ | |
{ | |
$match: { | |
activeJourneyId: { | |
$exists: true, | |
}, | |
}, | |
}, | |
{ | |
// query from a different collection !!! | |
$lookup: { | |
from: "groups", | |
localField: "activeGroupId", | |
foreignField: "_id", | |
as: "group", | |
}, | |
}, | |
{ | |
$unwind: { | |
path: "$group", | |
preserveNullAndEmptyArrays: true, | |
}, | |
}, | |
{ | |
$lookup: { | |
// query from a different collection !!! | |
from: "questions", | |
localField: "_id", | |
foreignField: "userId", | |
as: "questions", | |
}, | |
}, | |
{ | |
$project: { | |
lastName: 1, | |
lastLogin: 1, | |
_createdDate: 1, | |
bio: 1, | |
trip: 1, | |
_lastUpdatedDate: 1, | |
nickname: 1, | |
lastProfileUpdate: 1, | |
ifVisited: 1, | |
email: 1, | |
thumbnail: 1, | |
hometown: 1, | |
activeGroupId: 1, | |
activeJourneyId: 1, | |
activeChannelUrl: 1, | |
fullName: 1, | |
dateOfBirth: 1, | |
firstName: 1, | |
userPhotos: 1, | |
registrationToken: 1, | |
location: 1, | |
activeJourneyName: 1, | |
group: { | |
_id: "$group._id", | |
name: "$group.name", | |
description: "$group.description", | |
}, | |
questions: 1, | |
}, | |
}, | |
{ | |
$count: "userCount", | |
}, | |
] | |
export const aggCall = async () => { | |
const res = await db.collection("userInfo").aggregationPipeline(aggSteps).get(); | |
console.log(JSON.stringify({ ...res.data }, null, 2)); | |
/* | |
expected output: | |
{ | |
"0": { | |
"badges": [], | |
"ifVisited": false, | |
"image": { | |
"imageName": "", | |
"altText": "", | |
"imageURL": "", | |
"extension": "" | |
}, | |
"isAdmin": false, | |
"trip": [], | |
"userCount": 1846 | |
} | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment