我有 2 個要使用查找組合的集合。
Collection1:_AddressSyncStatus 欄位我想從這個集合中使用:“地址”
Collection2:EthTokenTransfers 我想從這個集合中使用的欄位:“to_address”、“from_address”。
現在,當我使用 mongo compass 時,查找過濾器需要一個本地欄位,在我的例子中是 EthTokenTransfers 的本地欄位來加入集合。我現在的問題是,我想查找 _AddressSyncStatus 中的地址是 EthTokenTransfers.from_address 還是 EthTokenTransfers.to_address。
這可以使用聚合嗎?
{
from: '_AddressSyncStatus',
localField: 'string', //from_address OR to_address
foreignField: 'address',
as: 'synced_address'
}
uj5u.com熱心網友回復:
一種方法是使用$lookup管道$or:
db.EthTokenTransfers.aggregate([
{
$lookup: {
from: "_AddressSyncStatus",
let: {
from_address: "$from_address",
to_address: "$to_address"
},
pipeline: [
{
$match: {
$expr: {$or: [{$eq: ["$_id", "$$from_address"]},
{$eq: ["$_id", "$$to_address"]}
]
}
}
}
],
as: "synced_address"
}
}
])
正如你在這里看到的。
但我認為對于使用這些資料的作業的 rset,這樣會更方便:
db.EthTokenTransfers.aggregate([
{
$lookup: {
from: "_AddressSyncStatus",
localField: "from_address",
foreignField: "_id",
as: "synced_address_from"
}
},
{
$lookup: {
from: "_AddressSyncStatus",
localField: "to_address",
foreignField: "_id",
as: "synced_address_to"
}
}
])
正如你在這里看到的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470071.html
標籤:mongodb
