[英]How can I sort data on Mongodb
Here is data from MongoDB 这是来自MongoDB的数据
{
"urls" : {
"beauty-credit" : {
"count" : 1,
"keyword" : "beauty credit",
"last_res" : 152,
"last_sea" : "Sat May 13 2017 15:16:41 GMT+0700 (SE Asia Standard Time)",
"url_site" : "beauty-credit"
},
"etude" : {
"count" : 2,
"keyword" : "etude",
"last_res" : 1048,
"last_sea" : "Sat May 13 2017 15:16:38 GMT+0700 (SE Asia Standard Time)",
"url_site" : "etude"
},
"skinfood" : {
"count" : 2,
"keyword" : "skinfood",
"last_res" : 478,
"last_sea" : "Sat May 13 2017 15:16:45 GMT+0700 (SE Asia Standard Time)",
"url_site" : "skinfood"
}
}
}
and Here is my code. 这是我的代码。 Now I filter only last_res > 10 现在我只过滤last_res> 10
function gotData(data){
result = data.val()
const urls_kws = Object.keys(result)
.filter(key => result[key].last_res > 10)
}
How can I sort data by "count"? 如何按“计数”对数据排序? Also how can I show only 30 rows? 另外我怎么只显示30行?
In order to sort the data by count you can use the sort() function from javascript with a custom comparator like 为了按计数对数据进行排序,您可以将javascript中的sort()函数与自定义比较器一起使用
urls_kws.sort((a, b) => a.count - b.count)
In order to now get only the first 30 values, you can then slice the array 为了现在只获取前30个值,您可以对数组进行切片
var short_urls_kws = urls_kws.slice(0, 30)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.