08.Mongo DB - Query part 4 (Query Example)

   



Project can be download from 


db.transaction.aggregate([
    {
       $match: {
            "clientId":{$exists: true,$eq: "080c99df-22c4-4744-86e4-bfdfde2a5794"},
            "checks.customer.incentivioId":{$exists: true},
            "businessDate":{$gt: new Date('2019-07-08 00:00:00.000Z'),$lt: new Date('2023-07-08 00:00:00.000Z')}
        }
    },
   /* {  $match: {$and :[
                {"clientId":{$exists: true,$eq: "080c99df-22c4-4744-86e4-bfdfde2a5794"}},
                {"checks.customer.incentivioId":{$exists: true}},
                {"businessDate":{$gt: new Date('2019-07-08 00:00:00.000Z'),$lt: new Date('2023-07-08 00:00:00.000Z')}}        
                    ]
            }
    },*/
    {
       $group: {
               _id: "$checks.customer.incentivioId",
               noOfTransactions: { $sum: 1 },
               totalBasketValue: { $sum: "$checks.total.total" },
               //totalBasketValuee: { $cond: {$if:{$and:[{$gt:[{$sum: "$checks.total.total"},10]},{$lt:[{$sum: "$checks.total.total"},100]}]},$then: 30, $else: 20}},
               averageBasketValue: { $avg: "$checks.total.total" },
               lastPurchaseDate: { $max: "$businessDate" },
               firstPurchaseDate: { $min: "$businessDate" },
               clientId: { $first: "clientId" },
               uniqueExtTxIdSet: { $addToSet: "extTxId" }
            }
   },
   {
       $limit: 100
   },
   {
       $sort: {noOfTransactions: 1,totalBasketValue:-1} // 1 - accending, -1 - decending
   },
   {
       $project: {// 1 - include , 0 - exclude
           _id: 0,
           info:{
               noOfTransactions:"noOfTransactions",
               averageBasketValue:"averageBasketValue"
               }
           }
   },
   {
       $skip: 10
   }
   
])





db.workouts.aggregate([
    { "$match": { "user_id": ObjectId("....") }},
    { "$project": {
       "20": { "$cond": [
           { "$and": [
               { "$gt": [ "$avg_intensity", 20 ] },
               { "$lt": [ "$avg_intensity", 25 ] }
           ]},    
           "$total_volume",
           0
       ]}
   }}
])

.

Comments

Popular posts from this blog

02. Spring – Creating spring project clone it with GIT step by step.

09.Mongo DB - ALL

11. Docker – Implementation => CONTAINER LINK WITH MYSQL.