我是 mongo 查询的新手。目前我有一个这样的集合,用于创建 d3 力导向图。
{
"_id": "allesgute3",
"nodes": [{
"id": "bmw@gmail.com",
"count": 15,
"nodeUpdatetime": 1525341732
}, {
"id": "abc@gmail.com",
"count": 10,
"nodeUpdatetime": null
}, {
"id": "xyz@gmail.com",
"count": 8,
"nodeUpdatetime": 1525408742
}, {
"id": "wilson@gmail.com",
"count": 4,
"nodeUpdatetime": 1525423847
}, {
"id": "niv@gmail.com",
"count": 6,
"nodeUpdatetime": 1525447758
}, {
"id": "car@gmail.com",
"count": 9,
"nodeUpdatetime": 1525447763
},
{
"id": "jason@gmail.com",
"count": 1,
"nodeUpdatetime": 1525447783
}
],
"links": [{
"source": "bmw@gmail.com",
"target": "jason@gmail.com",
"timestamp": 1525312111
}, {
"source": "car@gmail.com",
"target": "jason@gmail.com",
"timestamp": 1525334013
}, {
"source": "bmw@gmail.com",
"target": "car@gmail.com",
"timestamp": 1525334118
}]
}
使用 mongo 查询,我想生成这样的输出。基本上对于“节点”下的嵌套数据,添加一个名为“topn”的新字段,并按从 1 到 5 的计数对其进行排名。其余值为空。任何人都可以帮忙吗?谢谢!
{
"_id": "allesgute3",
"nodes": [{
"id": "bmw@gmail.com",
"count": 15,
"nodeUpdatetime": 1525341732,
"topn": 1
}, {
"id": "abc@gmail.com",
"count": 10,
"nodeUpdatetime": null,
"topn": 2
}, {
"id": "xyz@gmail.com",
"count": 8,
"nodeUpdatetime": 1525408742,
"topn": 4
}, {
"id": "wilson@gmail.com",
"count": 4,
"nodeUpdatetime": 1525423847,
"topn": null
}, {
"id": "niv@gmail.com",
"count": 6,
"nodeUpdatetime": 1525447758,
"topn": 5
}, {
"id": "car@gmail.com",
"count": 9,
"nodeUpdatetime": 1525447763,
"topn": 3
},
..............