Монго запрос: добавить новое поле с номером рейтинга на основе другого поля
Я новичок в монго запросах. В настоящее время у меня есть такая коллекция, которая используется для создания форсированного графа 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
}]
}
Используя запрос на монго, я хотел бы сгенерировать вывод примерно так. В основном для вложенных данных в "узлах", добавьте новое поле с именем "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
},
..............
1 ответ
Следующее должно дать вам то, что вы хотите:
db.collection.aggregate({
$unwind: "$nodes" // flatten the "nodes" array
}, {
$sort: { "nodes.count": -1 } // sort descending by "count"
}, {
$group: { // create the original structure again - just with sorted array elements
_id: "$_id",
nodes: { "$push": "$nodes" }
}
}, {
$addFields: {
"nodes": {
$zip: { // zip two arrays together
inputs: [
"$nodes", // the first one being the existing and now sorted "nodes" array
{ $range: [ 1, 6 ] } // and the second one being [ 1, 2, 3, 4, 5 ]
],
useLongestLength: true // do not stop after five elements but instead continue using a "null" value
}
}
}
}, {
$addFields: {
"nodes": {
$map: { // transform the "nodes" array
input: "$nodes",
as: "this",
in: {
$mergeObjects: [ // by merging two objects
{ $arrayElemAt: [ "$$this", 0] }, // the first sits at array position 0
{
topn: { $arrayElemAt: [ "$$this", 1] } // the second will be a new entity witha a "topn" field holding the second element in the array
}
]
}
}
}
}
})