Skip to content

Instantly share code, notes, and snippets.

@VanTigranyan
Created May 29, 2019 14:12
Show Gist options
  • Select an option

  • Save VanTigranyan/635af42959b4b4baa5917e03bd0f0b32 to your computer and use it in GitHub Desktop.

Select an option

Save VanTigranyan/635af42959b4b4baa5917e03bd0f0b32 to your computer and use it in GitHub Desktop.
mongo aggregation
export function fetchRequestedKernelsList(page: number, rfxType, requestStatus) {
const match = {
$or: [
{
rfxType: rfxType,
},
{
rfxType: 'both',
},
],
inactive: {$ne: true},
requestStatus: requestStatus,
};
if (rfxType !== 'rfp' && rfxType !== 'rfi' && rfxType !== 'both') {
delete match.$or;
}
if (requestStatus !== 'requested' && requestStatus !== 'in_progress' && requestStatus !== 'completed' ) {
match.requestStatus = { $exists: true };
}
return Promise.await(KernelCollection.collection['aggregate']([
{
$match: match,
},
{
$lookup: {
as: 'enterprise',
foreignField: '_id',
from: 'enterprises',
localField: 'eId',
},
},
{
$unwind: {
path: '$enterprise',
preserveNullAndEmptyArrays: true,
},
},
{
$project: {
_id: 1,
createdAt: 1,
enterprise: {
_id: 1,
name: 1,
},
name: 1,
requestStatus: 1,
rfxType: 1,
},
},
{
$skip: ( page - 1) * 20,
},
{
$limit: 20,
},
{
$sort: {
createdAt: -1,
},
},
]).toArray());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment