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.