用户表关联消费表
想查询3个月未消费的用户
match2的条件
顺便请教下 如下这种条件多的 如何优化?
match0 = {'$match': {'regDate': {'$gte': day_30, '$lt': today}}}
match1 = {'$match': {'consume.consumeDate': {'$gte': day_180, '$lte': today}}}
match2 = {'$match': {'consume': None}}
match3 = {'$match': {'recharge.rechargeDate': {'$gte': day_30, '$lte': today}}}
match4 = {'$match': {'recharge.tradeNo': {'$ne': ''}}}
lookup1 = {'$lookup': {
'from': 'consume',
'localField': '_id',
'foreignField': 'uid',
'as': 'consume'
}}
lookup2 = {'$lookup': {
'from': 'recharge',
'localField': '_id',
'foreignField': 'uid',
'as': 'recharge'
}}
project = {'$project': {
'_id': 1,
'regDate': 1,
'consume.amount': 1,
'consume.consumeDate': 1,
'recharge.real': 1,
'recharge.from': 1,
'recharge.rechargeDate': 1
}}
group = {'$group': {
'_id': {
'_id': '$_id',
}
}}
pipeline = [match0, lookup1, lookup2, match1, match2, match3, match4, project, group]
result = col_user.aggregate(pipeline)
LossUser = 0
for _ in result:
LossUser = LossUser + 1
print(LossUser)
白板的微信
相关分类