使用 $lookup 在子数组上按外键进行操作。

huangapple go评论52阅读模式
英文:

use $lookup on sub array by foreign key

问题

Here is the translated content:

我要搜索的内容似乎非常简单,但我找到的一切都是针对稍微不同的配置,我很难理解它们是如何工作的。

简化的父文档模型

{
  parentId: string,
  votes: [
    { voteId: string, something: number }
  ]
}

简化的投票文档模型

{
  voteId: string,
  name: string
}

期望的结果

{
  parentId: string,
  votes: [
    { voteId: string, something: number, name: string }
  ]
}

所以基本上结果是在父文档的parent.votes文档上保留所有内容(voteIdsomething),但对于每个投票文档,我要合并来自投票集合的文档(voteIdname,...rest)。

我注意到一个空管道可以让我完成80%的工作,但我会失去之前存储在parent.votes文档上的值。

{
  $lookup: {
    from: 'registration',
    let: {
      votes: '$votes',
    },
    pipeline: [],
    as: 'votes',
  },
}
英文:

What I'm searching for seems pretty straightforward but everything I find is for a slightly different configuration and I'm having a hard time understanding how they work.

simplified parent document model

{
  parentId: string,
  votes: [
    { voteId: string, something: number }
  ]
}

simplified vote document model

{
  voteId: string,
  name: string
}

desired outcome

{
  parentId: string,
  votes: [
    { voteId: string, something: number, name: string }
  ]
}

So basically the outcome is that everything is left in tact on the parent.votes document (voteId, something), but for each vote document, I merge the document from the votes collection. (voteId, name, ...rest)

I noticed that an empty pipeline gets me 80% of the way there, but I lose the values previously stored on parent.votes document

{
  $lookup: {
    from: 'registration',
    let: {
      votes: '$votes',
    },
    pipeline: [],
    as: 'votes',
  },
},

答案1

得分: 1

这是一种可能的方法。要“合并”registration集合中的所有字段,请参见下方。

registration集合合并所有字段

如果您想将registration集合中的所有字段合并到votes对象中,您可以简化上面的管道并执行以下操作。

请在mongoplayground.net上尝试。

英文:

Here's one way you could do it. To "merge" all fields from the registration collection, see below.

db.candidates.aggregate([
  {
    "$lookup": {
      "from": "registration",
      "localField": "votes.voteId",
      "foreignField": "voteId",
      "as": "voters"
    }
  },
  {
    "$set": {
      "votes": {
        "$map": {
          "input": "$votes",
          "as": "vote",
          "in": {
            "$mergeObjects": [
              "$$vote",
              {
                "name": {
                  "$getField": {
                    "field": "name",
                    "input": {
                      "$first": {
                        "$filter": {
                          "input": "$voters",
                          "as": "voter",
                          "cond": {"$eq": ["$$vote.voteId", "$$voter.voteId"]}
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  {"$unset": "voters"}
])

Try it on [mongoplayground.net](https://mongoplayground.net/p/Qm7JrrBubdT "Click me!").

Merging all fields from the registration collection

If you want to merge all fields from the registration collection into the votes object, you could simplify the above pipeline and do this.

db.candidates.aggregate([
  {
    "$lookup": {
      "from": "registration",
      "localField": "votes.voteId",
      "foreignField": "voteId",
      "as": "voters"
    }
  },
  {
    "$set": {
      "votes": {
        "$map": {
          "input": "$votes",
          "as": "vote",
          "in": {
            "$mergeObjects": [
              "$$vote",
              {
                "$first": {
                  "$filter": {
                    "input": "$voters",
                    "as": "voter",
                    "cond": {"$eq": ["$$vote.voteId", "$$voter.voteId"]}
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  {"$unset": "voters"}
])

Try it on [mongoplayground.net](https://mongoplayground.net/p/ossfaIEtn6c "Click me too!").

huangapple
  • 本文由 发表于 2023年5月7日 04:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定