简体   繁体   English

如何将 _id 与嵌套的 _id MongoDB 连接起来

[英]How to join _id with nested _id MongoDB

I'm working on project that required to joined nested _id with and _id for exemple I have我正在研究需要将嵌套 _id 和 _id 加入的项目,例如我有

Collection TPE looks like this系列 TPE 看起来像这样

{
sn:[{sn:0001,TPE_id:01},{sn:0001,TPE_id:02},{sn:0001,TPE_id:03},{sn:0001,TPE_id:04}],
sellerName:'XXX',
networkType:'GPRS'
}

And I have collection transaction that looks like this我的收藏交易看起来像这样

{
TPE_id:01,
transactionAmount:'XXX',
transactionType:'DECLINED'
}
{
TPE_id:01,
transactionAmount:'XXX',
transactionType:'APPROVED'
}
{
TPE_id:02,
transactionAmount:'XXX',
transactionType:'APPROVED'
}
{
TPE_id:04,
transactionAmount:'XXX',
transactionType:'CANCELED'
}

How can I join to get the transaction of related TPE_id for exemple例如,如何加入以获取相关 TPE_id 的交易

newArray 1 : [ {
    TPE_id:01,
    transactionAmount:'XXX',
    transactionType:'DECLINED'
    }
    {
    TPE_id:01,
    transactionAmount:'XXX',
    transactionType:'APPROVED'
    }]
newArray2 [{
    TPE_id:04,
    transactionAmount:'XXX',
    transactionType:'CANCELED'
    }]
newArray3 [  {
    TPE_id:02,
    transactionAmount:'XXX',
    transactionType:'APPROVED'
    }]

Questions问题

Can i do a loop through sn array to martch all the TPE_id with their related transaction ?我可以通过 sn 数组循环以将所有 TPE_id 与其相关事务进行匹配吗?
In case I have multiple Sn each Sn has array of TPE_id I want to get sn of specific id ?如果我有多个 Sn,每个 Sn 都有 TPE_id 数组,我想获得特定 id 的 sn 吗?
Sorry for my bad english对不起,我的英语不好

Here are the hints以下是提示

  1. Unwind sn field of TPE collection , it will restructure your array documents展开TPE collectionsn字段,它将重组您的数组文档
  2. Then do a $lookup on Transactions collection然后对Transactions集合进行$lookup
  3. Use project to get the desired o/p format使用project获取所需的 o/p 格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM