Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- // Stage 1: Match Document
- {
- $match: {
- store: storeId,
- }
- },
- // Stage 2: Lookup OfferUsers
- {
- $lookup: {
- from: "offerusers",
- localField: "_id",
- foreignField: "offer",
- as: "customers"
- }
- },
- // Stage 3: Filter the interestedCustomer
- {
- $addFields: {
- interestedCustomers: {
- $filter: {
- input: "$customers",
- as: "customer",
- cond: {
- $and: [
- { $gte: ["$$customer.viewDate", startDate] },
- { $lte: ["$$customer.viewDate", endDate] }
- ]
- }
- }
- }
- }
- },
- // Stage 4: Add fields - footTraffic, sales
- {
- $addFields: {
- footTrafficStatus: {
- $filter: {
- input: "$customers",
- as: "customer",
- cond: {
- $and: [
- { $gte: ["$$customer.footTrafficDate", startDate] },
- { $lte: ["$$customer.footTrafficDate", endDate] }
- ]
- }
- }
- },
- salesStatus: {
- $filter: {
- input: "$customers",
- as: "customer",
- cond: {
- $and: [
- { $gte: ["$$customer.claimedAt", startDate] },
- { $lte: ["$$customer.claimedAt", endDate] }
- ]
- }
- }
- }
- }
- },
- // Stage 5: Add fields - interestedUsers, footTraffic, salesCount
- {
- $addFields: {
- interestedUsers: {
- $size: "$interestedCustomers"
- },
- footTraffic: {
- $size: "$footTrafficStatus"
- },
- sales: {
- $size: "$salesStatus"
- },
- }
- },
- // Stage 6: Add fields - amount conversionRate, revenue
- {
- $addFields: {
- salesStatus: {
- $map: {
- input: "$salesStatus",
- as: "status",
- in: {
- $mergeObjects: [
- "$$status",
- {
- amount: {
- $cond: {
- if: { $eq: ['$__t', 'ParticularProduct'] },
- then: "$discountedPrice",
- else: "$minimumBillAmount",
- }
- }
- }
- ]
- }
- }
- },
- conversionRate: {
- $cond: {
- if: {
- $eq: ["$interestedUsers", 0]
- },
- then: 0,
- else: {
- $multiply: [
- {
- $divide: [
- "$sales", {
- $max: [1, "$interestedUsers"]
- }
- ]
- },
- 100
- ]
- }
- }
- },
- revenue: {
- $cond: {
- if: {
- $eq: ["$sales", 0]
- },
- then: 0,
- else: {
- $cond: {
- if: { $eq: ['$__t', 'ParticularProduct'] },
- then: { $multiply: ["$sales", "$discountedPrice"] },
- else: { $multiply: ["$sales", "$minimumBillAmount"] }
- }
- }
- }
- }
- }
- },
- // Stage 7: Group by _id to merge salesStatus objects into an array
- {
- $group: {
- _id: null,
- interestedUsers: { $sum: "$interestedUsers" },
- footTraffic: { $sum: "$footTraffic" },
- sales: { $sum: "$sales" },
- revenue: { $sum: "$revenue" },
- conversionRate: { $avg: "$conversionRate" },
- interestedCustomers: { $push: "$interestedCustomers" },
- footTrafficStatus: { $push: "$footTrafficStatus" },
- salesStatus: { $push: "$salesStatus" }
- }
- },
- // Stage 8: Add objects in interestedCustomers, footTraffic, salesStatus array to the root level
- {
- $addFields: {
- interestedCustomers: {
- $reduce: {
- input: "$interestedCustomers",
- initialValue: [],
- in: { $concatArrays: ["$$value", "$$this"] }
- }
- },
- footTrafficStatus: {
- $reduce: {
- input: "$footTrafficStatus",
- initialValue: [],
- in: { $concatArrays: ["$$value", "$$this"] }
- }
- },
- salesStatus: {
- $reduce: {
- input: "$salesStatus",
- initialValue: [],
- in: { $concatArrays: ["$$value", "$$this"] }
- }
- }
- }
- },
- // Stage 9: Add field - visitedStore, madePurchase in the interestedCustomers & madePurchase in the footTrafficStatus
- {
- $addFields: {
- interestedCustomers: {
- $map: {
- input: "$interestedCustomers",
- as: "status",
- in: {
- $mergeObjects: [
- "$$status",
- {
- visitedStore: {
- $cond: {
- if: { $ifNull: ["$$status.footTrafficDate", false] },
- then: true,
- else: false
- }
- },
- madePurchase: {
- $cond: {
- if: { $ifNull: ["$$status.claimedAt", false] },
- then: true,
- else: false
- }
- }
- }
- ]
- }
- }
- },
- footTrafficStatus: {
- $map: {
- input: "$footTrafficStatus",
- as: "status",
- in: {
- $cond: {
- if: { $ifNull: ["$$status.claimedAt", false] },
- then: {
- $mergeObjects: [
- "$$status",
- { madePurchase: true }
- ]
- },
- else: {
- $mergeObjects: [
- "$$status",
- { madePurchase: false }
- ]
- }
- }
- }
- }
- }
- }
- },
- // Stage 10: Exclude specific fields from the interestedCustomers, footTrafficStatus and salesStatus array
- {
- $project: {
- validOffersCount: 1,
- interestedUsers: 1,
- footTraffic: 1,
- sales: 1,
- revenue: 1,
- conversionRate: 1,
- interestedCustomers: {
- $map: {
- input: "$interestedCustomers",
- as: "customer",
- in: {
- user: "$$customer.user",
- timeOfClicking: "$$customer.viewDate",
- visitedStore: "$$customer.visitedStore",
- madePurchase: "$$customer.madePurchase",
- }
- }
- },
- footTrafficStatus: {
- $map: {
- input: "$footTrafficStatus",
- as: "customer",
- in: {
- user: "$$customer.user",
- visitedTime: "$$customer.footTrafficDate",
- madePurchase: "$$customer.madePurchase"
- }
- }
- },
- salesStatus: {
- $map: {
- input: "$salesStatus",
- as: "customer",
- in: {
- user: "$$customer.user",
- purchasedAt: "$$customer.claimedAt",
- amount: "$$customer.amount"
- }
- }
- },
- }
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement