Skip to content

Instantly share code, notes, and snippets.

@aonrobot
Created March 9, 2021 04:05
Show Gist options
  • Save aonrobot/393915581ef15a9f1ba6bb2d1c939992 to your computer and use it in GitHub Desktop.
Save aonrobot/393915581ef15a9f1ba6bb2d1c939992 to your computer and use it in GitHub Desktop.

MongoDB Meeting

Mostly used collection

1. Bettransactions

Game Callback (~15 games)

new BetTransactionModel(...)
// or
BetTransactionModel.updateOne({ ref1: ... }) (2 times per round)

Report Winlose/Account (/spa/report/wl_account_2)

BetTransactionModel.aggregate([
   {
      "$match":{
         "$and":[
            {
               "gameDate":{
                  "$gte":"2021-03-09T07:00:00.000Z",
                  "$lt":"2021-03-09T11:00:00.000Z"
               }
            },
            {
               "commission.superAdmin.group":"5a3"
            },
            {
               "commission.company.parentGroup":"2f2"
            },
            {
               "status":{
                  "$in":[
                     "DONE",
                     "REJECTED",
                     "CANCELLED"
                  ]
               }
            }
         ]
      }
   },
   {
      "$group":{
         "_id":{
            "groupId":"$commission.company.group"
         },
         "stackCount":{
            "$sum":1
         },
         "amount":{
            "$sum":"$amount"
         },
         "validAmount":{
            "$sum":"$validAmount"
         },
         "grossComm":{
            "$sum":{
               "$multiply":[
                  "$validAmount",
                  {
                     "$divide":[
                        "$commission.superAdmin.commission",
                        100
                     ]
                  }
               ]
            }
         },
         "memberWinLose":{
            "$sum":{
               "$toDecimal":"$commission.member.winLose"
            }
         },
         "memberWinLoseCom":{
            "$sum":{
               "$toDecimal":"$commission.member.winLoseCom"
            }
         },
         "agentWinLose":{
            "$sum":{
               "$toDecimal":"$commission.agent.winLose"
            }
         },
         "agentWinLoseCom":{
            "$sum":{
               "$toDecimal":"$commission.agent.winLoseCom"
            }
         },
         "masterAgentWinLose":{
            "$sum":{
               "$toDecimal":"$commission.masterAgent.winLose"
            }
         },
         "masterAgentWinLoseCom":{
            "$sum":{
               "$toDecimal":"$commission.masterAgent.winLoseCom"
            }
         },
         "seniorWinLose":{
            "$sum":{
               "$toDecimal":"$commission.senior.winLose"
            }
         },
         "seniorWinLoseCom":{
            "$sum":{
               "$toDecimal":"$commission.senior.winLoseCom"
            }
         },
         "shareHolderWinLose":{
            "$sum":{
               "$toDecimal":"$commission.shareHolder.winLose"
            }
         },
         "shareHolderWinLoseCom":{
            "$sum":{
               "$toDecimal":"$commission.shareHolder.winLoseCom"
            }
         },
         "companyWinLose":{
            "$sum":{
               "$toDecimal":"$commission.company.winLose"
            }
         },
         "companyWinLoseCom":{
            "$sum":{
               "$toDecimal":"$commission.company.winLoseCom"
            }
         },
         "superAdminWinLose":{
            "$sum":{
               "$toDecimal":"$commission.superAdmin.winLose"
            }
         },
         "superAdminWinLoseCom":{
            "$sum":{
               "$toDecimal":"$commission.superAdmin.winLoseCom"
            }
         }
      }
   },
   {
      "$project":{
         "_id":1,
         "type":"A_GROUP",
         "group":"$_id.groupId",
         "currency":"THB",
         "stackCount":"$stackCount",
         "grossComm":"$grossComm",
         "amount":"$amount",
         "validAmount":"$validAmount",
         "memberWinLose":{
            "$toDouble":"$memberWinLose"
         },
         "memberWinLoseCom":{
            "$toDouble":"$memberWinLoseCom"
         },
         "memberTotalWinLoseCom":{
            "$toDouble":{
               "$add":[
                  "$memberWinLose",
                  "$memberWinLoseCom"
               ]
            }
         },
         "agentWinLose":{
            "$toDouble":"$agentWinLose"
         },
         "agentWinLoseCom":{
            "$toDouble":"$agentWinLoseCom"
         },
         "agentTotalWinLoseCom":{
            "$toDouble":{
               "$add":[
                  "$agentWinLose",
                  "$agentWinLoseCom"
               ]
            }
         },
         "masterAgentWinLose":{
            "$toDouble":"$masterAgentWinLose"
         },
         "masterAgentWinLoseCom":{
            "$toDouble":"$masterAgentWinLoseCom"
         },
         "masterAgentTotalWinLoseCom":{
            "$toDouble":{
               "$add":[
                  "$masterAgentWinLose",
                  "$masterAgentWinLoseCom"
               ]
            }
         },
         "seniorWinLose":{
            "$toDouble":"$seniorWinLose"
         },
         "seniorWinLoseCom":{
            "$toDouble":"$seniorWinLoseCom"
         },
         "seniorTotalWinLoseCom":{
            "$toDouble":{
               "$add":[
                  "$seniorWinLose",
                  "$seniorWinLoseCom"
               ]
            }
         },
         "shareHolderWinLose":{
            "$toDouble":"$shareHolderWinLose"
         },
         "shareHolderWinLoseCom":{
            "$toDouble":"$shareHolderWinLoseCom"
         },
         "shareHolderTotalWinLoseCom":{
            "$toDouble":{
               "$add":[
                  "$shareHolderWinLose",
                  "$shareHolderWinLoseCom"
               ]
            }
         },
         "companyWinLose":{
            "$toDouble":"$companyWinLose"
         },
         "companyWinLoseCom":{
            "$toDouble":"$companyWinLoseCom"
         },
         "companyTotalWinLoseCom":{
            "$toDouble":{
               "$add":[
                  "$companyWinLose",
                  "$companyWinLoseCom"
               ]
            }
         },
         "superAdminWinLose":{
            "$toDouble":"$superAdminWinLose"
         },
         "superAdminWinLoseCom":{
            "$toDouble":"$superAdminWinLoseCom"
         },
         "superAdminTotalWinLoseCom":{
            "$toDouble":{
               "$add":[
                  "$superAdminWinLose",
                  "$superAdminWinLoseCom"
               ]
            }
         }
      }
   }
])
.read('secondaryPreferred')
.option({ maxTimeMS: 200000 })

Report match detail (/spa/report/wl_match_detail)

BetTransactionModel.aggregate([
   {
      "$match":{
         "gameDate":{
            "$gte":"2021-03-08T11:00:00.000Z",
            "$lt":"2021-03-09T11:00:00.000Z"
         },
         "commission.superAdmin.group":"5a3",
         "status":{
            "$in":[
               "DONE",
               "REJECTED",
               "CANCELLED"
            ]
         }
      }
   },
   {
      "$group":{
         "_id":{
            "game":"$game",
            "source":"$source",
            "leagueId":"$hdp.leagueId",
            "leagueName":"$hdp.leagueName",
            "matchName":"$hdp.matchName",
            "matchId":"$hdp.matchId",
            "oddType":"$hdp.oddType",
            "parlay":"$parlay",
            "gameType":"$gameType"
         },
         "stackCount":{
            "$sum":1
         },
         "amount":{
            "$sum":"$amount"
         },
         "grossComm":{
            "$sum":{
               "$multiply":[
                  "$amount",
                  {
                     "$divide":[
                        "$commission.superAdmin.commission",
                        100
                     ]
                  }
               ]
            }
         },
         "memberWinLose":{
            "$sum":"$commission.member.winLose"
         },
         "memberWinLoseCom":{
            "$sum":"$commission.member.winLoseCom"
         },
         "memberTotalWinLoseCom":{
            "$sum":"$commission.member.totalWinLoseCom"
         },
         "agentWinLose":{
            "$sum":"$commission.agent.winLose"
         },
         "agentWinLoseCom":{
            "$sum":"$commission.agent.winLoseCom"
         },
         "agentTotalWinLoseCom":{
            "$sum":"$commission.agent.totalWinLoseCom"
         },
         "masterAgentWinLose":{
            "$sum":"$commission.masterAgent.winLose"
         },
         "masterAgentWinLoseCom":{
            "$sum":"$commission.masterAgent.winLoseCom"
         },
         "masterAgentTotalWinLoseCom":{
            "$sum":"$commission.masterAgent.totalWinLoseCom"
         },
         "seniorWinLose":{
            "$sum":"$commission.senior.winLose"
         },
         "seniorWinLoseCom":{
            "$sum":"$commission.senior.winLoseCom"
         },
         "seniorTotalWinLoseCom":{
            "$sum":"$commission.senior.totalWinLoseCom"
         },
         "shareHolderWinLose":{
            "$sum":"$commission.shareHolder.winLose"
         },
         "shareHolderWinLoseCom":{
            "$sum":"$commission.shareHolder.winLoseCom"
         },
         "shareHolderTotalWinLoseCom":{
            "$sum":"$commission.shareHolder.totalWinLoseCom"
         },
         "companyWinLose":{
            "$sum":"$commission.company.winLose"
         },
         "companyWinLoseCom":{
            "$sum":"$commission.company.winLoseCom"
         },
         "companyTotalWinLoseCom":{
            "$sum":"$commission.company.totalWinLoseCom"
         },
         "superAdminWinLose":{
            "$sum":"$commission.superAdmin.winLose"
         },
         "superAdminWinLoseCom":{
            "$sum":"$commission.superAdmin.winLoseCom"
         },
         "superAdminTotalWinLoseCom":{
            "$sum":"$commission.superAdmin.totalWinLoseCom"
         }
      }
   },
   {
      "$project":{
         "_id":0,
         "key":"$_id",
         "source":"$source",
         "parlay":"$_id.parlay",
         "member":"$_id.member",
         "group":"$_id.commission",
         "stackCount":"$stackCount",
         "grossComm":"$grossComm",
         "amount":"$amount",
         "memberWinLose":"$memberWinLose",
         "memberWinLoseCom":"$memberWinLoseCom",
         "memberTotalWinLoseCom":"$memberTotalWinLoseCom",
         "agentWinLose":"$agentWinLose",
         "agentWinLoseCom":"$agentWinLoseCom",
         "agentTotalWinLoseCom":"$agentTotalWinLoseCom",
         "masterAgentWinLose":"$masterAgentWinLose",
         "masterAgentWinLoseCom":"$masterAgentWinLoseCom",
         "masterAgentTotalWinLoseCom":"$masterAgentTotalWinLoseCom",
         "seniorWinLose":"$seniorWinLose",
         "seniorWinLoseCom":"$seniorWinLoseCom",
         "seniorTotalWinLoseCom":"$seniorTotalWinLoseCom",
         "shareHolderWinLose":"$shareHolderWinLose",
         "shareHolderWinLoseCom":"$shareHolderWinLoseCom",
         "shareHolderTotalWinLoseCom":"$shareHolderTotalWinLoseCom",
         "companyWinLose":"$companyWinLose",
         "companyWinLoseCom":"$companyWinLoseCom",
         "companyTotalWinLoseCom":"$companyTotalWinLoseCom",
         "superAdminWinLose":"$superAdminWinLose",
         "superAdminWinLoseCom":"$superAdminWinLoseCom",
         "superAdminTotalWinLoseCom":"$superAdminTotalWinLoseCom"
      }
   }
])
.read('secondaryPreferred')

2. Updatebalancehistory2

Update member balance (2 times per round)

new UpdateBalanceHistoryModel(body)

MemberModel.findOneAndUpdate(
	{username_lower: username.toLowerCase()}, 
	{$inc: {balance: amount},lastUpdateBalance:Utils.Date.currentDate()}, 
	{new: true }
)

3. Members

Login (/member/login)

MemberModel.findOne(condition)
.select({
    _id: 1,
    contact: 1,
    username: 1,
    username_lower: 1,
    group: 1,
    currency: 1,
    limitSetting: 1,
    commissionSetting: 1,
    configuration: 1,
    balance: 1,
    termAndCondition: 1,
    forceChangePassword: 1,
    forceChangePasswordDate: 1,
    lock: 1,
    suspend: 1,
    isAutoTopUp: 1,
})

// There is Redis to cache this one. (TTL.120sec)
AgentGroupModel.findById(mongoose.Types.ObjectId(agentGroupId))
.select('_id lock suspend parentId limitSetting name_lower')
.populate({
    path: 'parentId',
    select: '_id lock suspend parentId limitSetting name_lower',
    populate: {
        path: 'parentId',
        select: '_id lock suspend parentId limitSetting name_lower',
        populate: {
            path: 'parentId',
            select: '_id lock suspend parentId limitSetting name_lower',
            populate: {
                path: 'parentId',
                select: '_id lock suspend parentId limitSetting name_lower',
                populate: {
                    path: 'parentId',
                    select: '_id lock suspend parentId limitSetting name_lower',
                }
            }
        }
    }
})

MemberModel.update({_id: user._id}, {
    $set: {
        ipAddress: clientIp,
        lastLogin: DateUtils.getCurrentDate()
    }
})

TokenModel.update({username: user.username}, {
    username: user.username,
    uniqueId: user.uid,
    type:'MEMBER',
    createdAt: DateUtils.getCurrentDate()
})

4. Agentgroups

Checking agent inactive status (A one off login into the game)

// There is Redis to cache this one. (TTL.120sec)
AgentGroupModel.findById(memberParentId)
.select("_id lock suspend active parentId limitSetting name_lower")
.populate({
    path: "parentId",
    select: "_id lock suspend active parentId limitSetting name_lower",
    populate: {
        path: "parentId",
        select: "_id lock suspend active parentId limitSetting name_lower",
        populate: {
        path: "parentId",
        select: "_id lock suspend active parentId limitSetting name_lower",
        populate: {
            path: "parentId",
            select: "_id lock suspend active parentId limitSetting name_lower",
            populate: {
            path: "parentId",
            select: "_id lock suspend active parentId limitSetting name_lower",
            },
        },
        },
    },
})
@karan9
Copy link

karan9 commented Mar 9, 2021

Alternative way of lookup

// Alternative way to lookup in Sharded Environments
// Current Method
Db.collection.aggregate([
  {
    $match: {}
  },
  {
    $lookup: {
      .... lookup condition ...
    }
  }
]);

// =-------------------=
// New Method

function myApi() {
  // First we will fetch the required documents from collection 1
  const documents: Array = await myAggregation();

  // Once we have fetched the documents from collection 1, we will issue
  // Second query that does a $match on the "LookedUp" Collection using the _id values OR Whatever match
  // value you have from collection1 to lookedup Collection
  const lookedUpValues = await Db.LookupCollection.aggregate([
    {
      $match: {
        _id: { "$in": documents.map(d => d._id)  }
      }
    }
  ]);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment