Pymongo aggregates the method for situations where the query condition is not in the table.

user table associated with consumption table
want to query the conditions of users who have not consumed for 3 months
match2
by the way, how to optimize the following conditions?

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)

May.05,2021

1.match2 condition what does this None mean? if you want to query whether the consume field exists, you can change it to

.
{'$match': {'consume': {$exist:true}}}

2. For this kind of optimization, I once asked the professional god @Mongoing Chinese community , I'll teach you a lesson. I guess this kind of query will be very slow, but the reason for the slowness is not that match is too slow, but because lookup is too slow, which affects efficiency. Mongodb is a non-relational database, and the design itself does not want relationships (I guess ha). So the relationship between lookup and other queries is not its strengths, and what you are querying here are consumption records and recharge records, which are data that may not be changed, so I suggest that you change the data structure, store consume and recharge directly under this user, and then filter directly without lookup, which will definitely be very fast, of course, if the business allows, modify the data structure.
below posted my eldest brother's answer
mongodb 's related query $lookup
ps: and I have done an experiment. When all 20 pieces of data need lookup, I check the database twice (first find out the data before lookup, then check the database again with the basis of lookup (for example, _ id), and then integrate the two results into the data I want) faster than direct lookup.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38e10-2c141.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38e10-2c141.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?