英文:
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
文档上保留所有内容(voteId
,something
),但对于每个投票文档,我要合并来自投票集合的文档(voteId
,name
,...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!").
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论