score:1
The following should get you what you want:
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
}
]
}
}
}
}
})
Source: stackoverflow.com
Related Query
- mongo query: add a new field with ranking number based on another field
- How do i add two different shapes to D3 forced directed graph based on shape field value?
- Add a link to another page with d3.js
- Redraw D3 Pie Chart with new data when clicking button or enter search field
- Add node on click on another node with d3js
- Add new row dynamically to an array with headers created from a csv file
- Creating a bar chart based on number or rows over 100k using javascript with d3?
- Mongo Query: flatten field and split into a new document
- How can I add a variable number of nodes with D3 append() function depending on data
- d3 add multiple classes with function
- D3: How to refresh a chart with new data?
- Dynamically resize the d3 tree layout based on number of childnodes
- Changing number displayed as svg text gradually, with D3 transition
- Format a number with SI Prefix, with fixed number of decimals
- How to update d3.js bar chart with new data
- Compare/Diff new data with previous data on d3.js update
- D3 using classed() to add and remove class with checkbox
- dc.js add class to data points in multiple charts based on criteria from first chart
- D3 updating graph with new elements create edges with the wrong nodes
- How to add a simple arc with D3
- Add a concurrent transition during another transition if a condition is met
- D3.js - How can I add a new line to the text in this Collapsible Tree?
- Plotting a very large number of points on HTML5 canvas with JavaScript
- Count the number of rows of a CSV file, with d3.js
- D3.js - how to add zoom button with the default wheelmouse zoom behavior
- d3 monthly data set - update data- add new group
- Mix bar and line charts with Rickshaw (d3 based graphing library for js)
- New layout after opening a group not base on the last layout with cola.js
- update d3 pie chart with new data.json
- d3.js chart not updated with new data
More Query from same tag
- Add tooltip on D3s x axis tick labels
- Multiple networks in 1 graph from networkD3's forceNetwork
- D3 - pass variable to x value
- Create matrix like HTML table with d3.js
- Cal-Heatmap display only 3 months in row another 3 months another row till all 12 months
- Remove animations for d3 force layout
- How to check with d3.js if element is in viewpoint (in visible area)
- Javascript - fade to display:none
- Preserve the size of the svg group when changing the canvas size
- Creating a basic bar chart in d3js
- Two-way arrows possible in force directed graph?
- d3-transition v2: Error: transition 1 not found
- D3 Geo Tile Baselayer Offset
- how to make smooth transitions with d3 + socket.io
- D3.js: Expand tree till node n
- can you call a function while loading data in D3 js
- Logarithmic scale returns NaN
- Jquery cannot invoke a click, but can manually do it with a mouse
- D3 - Resetting an SVG object animation
- mouseover on d3 force layout bubbles not working
- D3 Chart is not displaying
- How to make into d3.js v3.4 the same behavior drag as into v3.1?
- failed to parse x to Date object
- d3.js : getting the bars width or X position right?
- Make axis ticks strings in d3
- How to allow layout.pack() size to be free form in d3?
- Using variables to access nested json keys in d3
- Round x axis of a time scale to nearest half hour
- Using nested JSON data in D3
- TypeError: svg.append(...).attrs is not a function when creating charts using D3.js