Created
May 29, 2019 14:12
-
-
Save VanTigranyan/635af42959b4b4baa5917e03bd0f0b32 to your computer and use it in GitHub Desktop.
mongo aggregation
This file contains hidden or 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
| 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