How to associate query data with multiple tables through Pymongo?

MongoDB database multi-table query data

before, you could associate two tables with $lookup in Robo 3T

.

related codes

db.users.aggregate([
{    $match : 
    { regDate : { "$gte" : ISODate("2018-05-01T00:00:00Z"), "$lt" : ISODate("2018-05-31T00:00:00Z") } }
},
{
    $project :
    {    
        "_id":1,
        "regDate":1,
        "totalRecharge":1,
        "jcode":1
    }
},
{
$lookup:
    {
        "from":"recharge",
        "localField":"_id",
        "foreignField":"uid",
        "as":"recharge"
    }
},
{ $match : {"recharge.from" : "weixin"}},
{ $match : {"recharge.from" : "alipay"}},
{ $match : {"recharge.rechargeDate" : {"$gte" : ISODate("2018-05-01T00:00:00Z"), "$lt":ISODate("2018-05-31T00:00:00Z")}}},

])

users is the user table, and recharge is the recharge table. There is no recharge amount (real) in the user table and no user registration time in the (rechargeDate), recharge table.

want to find out which users are recharged and which have been recharged within the time range (registration for 1 week, 3 weeks, etc.).

Mar.28,2021

the easiest way is to redundancy the user registration information into the recharge record. There is no need for $lookup at all, and the performance can be greatly improved. After all, the registration time will not change.


this requirement is somewhat difficult. The complex query method of multi-table query in non-relational database is not worth mentioning, and this kind of query method should be avoided as far as possible. It is recommended to use it with ES or cache the query results.

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-1b390c0-34511.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-1b390c0-34511.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?