2
的计数创建新的领域我已经在我的数据库,为此我需要统计Tripduration
的,并且基于Tripdurations
数量逐一介绍各"EndStation"
,这给"Tripcount"
下面的一个新领域以下strucutre 。其他领域
"_id": 1,
"Tripcount": 4,
"Trips_out": [
{
"EndStation": 3119,
"Tripcount": // This should be 3
"Tripduration": [
544,
364,
34
]
},
{
"EndStation": 3081,
"Tripcount": // This should be 1
"Tripduration": [
835
]
}
我开始用下面的查询,但这个计算为所有tripdurations,它提供了Tripcount : 4
顶部:
db.mycollection.aggregate(
[
{
"$addFields":{
"Tripcount":{
"$reduce":{
"input":{
"$map":{
"input":"$Trips_out",
"in":{
"$size":"$$this.Tripduration"
}
}
},
"initialValue":0,
"in":{
"$add":[
"$$value",
"$$this"
]
}
}
}
}
}
]
)
怎么可以这样修改,使得它计数Tripdurations为每个EndStation,并将其作为新字段插入?
这是完美的,非常感谢。 – ffritz