【问题标题】:How do I join two documents by id如何通过 id 加入两个文档
【发布时间】:2020-09-17 22:12:12
【问题描述】:

我需要一些有关弹性搜索的帮助 我想加入两件事,第一件事的架构看起来像

统计组

{
    "_id": 38282922829,
    "statValues": [{
          _ref: 282828290,
     }]
}

统计数据

{
    "_id": 282828290,
    "key": itemsGained
}

如您所见,我想将 statvalues 中的所有 ref 与 stat 数据的 id 连接起来。我该怎么做?

【问题讨论】:

    标签: mongodb elasticsearch elastic-stack elasticsearch-5


    【解决方案1】:

    您需要使用$unwind 运算符和$lookup 运算符来执行此操作。

    db.STAT_GROUP.aggregate([
      {
         $unwind: '$statValues'
      },
      {
         $lookup: {
           from: 'STAT_DATA',
           localField: 'statValues._ref',
           foreignField: '_id',
           as: 'Refs'
         }
      }
    ])
    

    MongoPlayGroundLink

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多