Skip to content

Instantly share code, notes, and snippets.

@nanthanwa
Created September 11, 2021 21:58
Show Gist options
  • Save nanthanwa/54d4ca654ba4951ae5c693d20ab0e2a4 to your computer and use it in GitHub Desktop.
Save nanthanwa/54d4ca654ba4951ae5c693d20ab0e2a4 to your computer and use it in GitHub Desktop.
users = await UserMobile.aggregate([
{
$match: query,
},
{
$lookup: { from: 'userpackages', localField: 'userPackage', foreignField: '_id', as: 'userPackage' },
},
{
$lookup: { from: 'dates', localField: '_id', foreignField: 'owner', as: 'activeDate' },
},
{
$lookup: { from: 'dates', localField: '_id', foreignField: 'owner', as: 'dateJoined' },
},
{
$lookup: { from: 'dates', localField: '_id', foreignField: 'accepted.uid', as: 'dateMatch' },
},
{
$lookup: { from: 'log_swipe', localField: '_id', foreignField: 'srcUser', as: 'swipe' },
},
{
$project: {
introduce: 1,
gender: 1,
birthDay: 1,
displayName: 1,
photos: 1,
email: 1,
createdAt: 1,
updatedAt: 1,
location: {
$cond: {
if: '$enableCustomLoc',
then: {
$cond: {
if: '$customLocation.province',
then: '$customLocation.province',
else: '$customLocation.loc.coordinates',
},
},
else: {
$cond: {
if: '$location.province',
then: '$location.province',
else: '$location.loc.coordinates',
},
},
},
},
userPackage: { $arrayElemAt: ['$userPackage.name', 0] },
activeDate: { $size: '$activeDate' },
dateJoined: { $add: [{ $size: '$dateJoined.applicants' }, { $size: '$dateJoined.accepted' }] },
dateMatch: { $size: '$dateMatch' },
swipe: { $size: '$swipe' },
match: { $size: '$matched' },
},
},
{ $sort: sort },
{ $skip: cursor },
{ $limit: limit },
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment