zimovid

Lookup power

Mar 10th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  const pipeline: object[] = [
  2.             {
  3.                 $match: {
  4.                     active: true,
  5.                     platformId: new mongodb.ObjectID(platformId),
  6.                 },
  7.             },
  8.             {
  9.                 $lookup: {
  10.                     from: 'retailers',
  11.                     localField: 'retailerId',
  12.                     foreignField: '_id',
  13.                     as: 'retailer',
  14.                 },
  15.             },
  16.             {
  17.                 $unwind: {
  18.                     path: '$retailer',
  19.                 }
  20.             },
  21.             {
  22.                 $match: {
  23.                     'retailer.active': true,
  24.                 }
  25.             },
  26.             {
  27.                 $lookup: {
  28.                     from: 'dictionaryretaileroptions',
  29.                     localField: 'optionListId',
  30.                     foreignField: '_id',
  31.                     as: 'optionList',
  32.                 },
  33.             },
  34.             {
  35.                 $lookup: {
  36.                     from: 'coupons',
  37.                     let: {'retailerOnPlatformId': '$_id'},
  38.                     pipeline: [
  39.                         {
  40.                             $match: {
  41.                                 $expr: {
  42.                                     $and: [
  43.                                         {$eq: ['$retailerOnPlatformId', '$$retailerOnPlatformId']},
  44.                                         {$eq: ['$active', true]},
  45.                                         {$lte:['$dateStart',  currDate]},
  46.                                         {$gte:['$dateEnd',  currDate]},
  47.                                     ]
  48.                                 }
  49.                             }
  50.                         }
  51.                     ],
  52.                     as: 'coupons',
  53.                 },
  54.             },
  55.             {
  56.                 $match: {
  57.                     ...match,
  58.                 },
  59.             },
  60.             {
  61.                 $limit: limit
  62.             },
  63.             {
  64.                 $project: {
  65.                     slug: 1,
  66.                     imageText: 1,
  67.                     retailerId: '$retailer._id',
  68.                     name: '$retailer.name',
  69.                     alternativeName: '$retailer.alternativeName',
  70.                     image: '$retailer.image',
  71.                     categoryListId: '$retailer.categoryListId',
  72.                     sliderImage: 1,
  73.                     // bannerImage: 1,
  74.                     landingsSort: 1,
  75.                     couponCount: {$size: '$coupons'},
  76.                 },
  77.             },
  78.             // {$match: {'couponCount': {$gt: 0}}}
  79.         ];
Advertisement
Add Comment
Please, Sign In to add comment