Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Query {
- page?: number,
- limit?: number,
- otherFeild1?: string,
- sort?: number,
- sortBasedOn?: Array<'totalScore' | 'doneProjects' | 'workExperience' | 'commentNumber' | 'createdAt'>,
- }
- const queryParameters: Query = {
- page,
- limit,
- otherFeild1,
- sort,
- sortBasedOn,
- };
- const $match = {
- ...(otherFeild1 ? { otherFeild1 } : {})
- }
- const lookups = [
- {
- $lookup: {
- from: 'collection-name1',
- localField: 'collection-name1Id',
- foreignField: '_id',
- as: 'collection-name1',
- },
- },
- {
- $unwind: {
- path: '$collection-name1',
- },
- },
- ];
- const $project = {
- 'field1.subField1': -1,
- 'field2': -1,
- };
- const $facet = {
- documents: [
- {
- $sort: {
- ...(sortBasedOn.length > 0
- ? sortBasedOn.map((field) => ({ [field]: sort }))
- : { createdAt: -1 }),
- },
- },
- { $skip: limit ?? * (page ?? 1 - 1) },
- { $limit: limit ?? 10 },
- ],
- };
- const schemaDocuments: { documents: any[] }[] = await SchemaName.aggregate([
- ...(Object.keys($match).length > 0 ? [{ $match }] : []),
- ...lookups,
- { $project },
- { $facet },
- ]);
- console.log(
- schemaDocuments[0].documents
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement