我使用的是MongoDB聚合框架。我有一个Mongo集合,里面有这样的文档:
{
'step': 1,
'name': 'house',
'score': 2
}
{
'step': 1,
'name': 'car',
'score': 3
}
{
'step': 2,
'name': 'house',
'score': 4
}我正在使用相同的'step‘对文档进行分组,并将'name’和'score‘推入一个对象数组。我得到的是:
{
'step': 1,
'scores':
[
{'name':'house','score':2},
{'name':'car','score':3}
]
}
{
'step': 2,
'scores':
[
{'name':'house','score':4}
]
}对于每个'step‘,我需要复制前一个'step’的值,以防'name‘不存在。我应该有这样的东西:
{
'step': 1,
'scores':
[
{'name':'house','score':2},
{'name':'car','score':3}
]
}
{
'step': 2,
'scores':
[
{'name':'house','score':4},
**{'name': 'car', 'score':3}**
]
}在第二个文档中,元素{'name': 'car‘,'score':3}已从前一个文档中复制出来,因为在'step:2’中没有文档‘car’具有'score‘。
我不知道如何使用MongoDB聚合来执行此操作。如果能帮上忙,我们将非常感激。
发布于 2020-08-06 17:06:59
需要在管道中使用$lookup,请一步一步地看下面。
$group by step和push all name in one array scoresname in names of each step,我们将在匹配条件中使用查找db.collection.aggregate([
{
$group: {
_id: "$step",
scores: {
$push: {
name: "$name",
score: "$score"
}
},
names: { $push: "$name" }
}
},$unwind scores,因为它是数组,我们将查找 { $unwind: "$scores" },$lookup让流水线的变量step(_id)和names使用表达式$expr有3个条件names的大小它应该是一个(1),它的car或house,not in。如果car已经可用,那么它将在查找中搜索house,需要使用单独的$not和$in显示所需fields
$project将存储在clone_score中
{
$lookup: {
from: "collection",
let: {
step_id: { $subtract: ["$_id", 1] },
names: "$names"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: [{ $size: "$$names" }, 1] },
{ $eq: ["$$step_id", "$step"] },
{ $not: [{ $in: ["$name", "$$names"] }] }
]
}
}
},
{
$project: {
_id: 0,
name: 1,
score: 1
}
}
],
as: "clone_score"
}
},$group by clone_score ( step(_id) )并将所有分数推入一个数组中,保留first scores
{
$group: {
_id: "$_id",
scores: { $push: "$scores" },
clone_score: { $first: "$clone_score" }
}
},在上面的管道中,我们现在有两个独立的数组
scores和clone_score,我们需要在scores中将它们连接起来
{
$project: {
_id: 0,
step: "$_id",
scores: {
$concatArrays: ["$scores", "$clone_score"]
}
}
}
])https://stackoverflow.com/questions/63280021
复制相似问题