在下面的集合中,我想尝试从coin_infos
_id
coin_infos
coin_id
wallet_infos
user_id
_id:objectID(5a4b07b2a0050c20a6be44b3),
coin_code:BTC,
wallet_name:bitcoin,
deposite_txn_fee:3,
min_withdrawn:5,
withdrawn_txn_fee:0.001,
和 _id:objectID(5a58a4e222068e053d71220d),
user_id:5a4b0787a0050c20a6be44b2,
coin_id:5a4b07b2a0050c20a6be44b3,
balance:3.122858,
获取所有记录}。
coin_infos集合
{
_id:objectID(5a4b07b2a0050c20a6be44b3),
coin_code:BTC,
wallet_balance:3.122858,
wallet_name:bitcoin,
deposite_txn_fee:3,
min_withdrawn:5,
withdrawn_txn_fee:0.001,
"match_records" : [
_id:objectID(5a58a4e222068e053d71220d),
user_id:5a4b0787a0050c20a6be44b2,
coin_id:5a4b07b2a0050c20a6be44b3,
balance:3.122858,
]
}
wallet_infos集合
{{1}}
我有用户名 - :5a4b0787a0050c20a6be44b2
输出如下:..
{{1}}
我的mongoDB版本 3.2.11。
答案 0 :(得分:2)
您可以使用$lookup
和$match
聚合管道,就像您已经在mongo版本3.2 +
$lookup
将coin_infos
集合与wallet_infos
coin_id
加入,如果找到匹配,则会将钱包信息作为嵌入文档返回,来自嵌入式文档user_id即可执行$match
并过滤
db.coin_infos.aggregate(
[
{
$lookup : {
from : "wallet_infos",
localField : "_id",
foreignField: "coin_id",
as : "walletInfo"
}
},
{$match : {"walletInfo.userId" : "5a4b0787a0050c20a6be44b2"}}
]
)