简体   繁体   中英

How can I sort data on Mongodb

Here is data from 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

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?

In order to sort the data by count you can use the sort() function from javascript with a custom comparator like

 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

 var short_urls_kws = urls_kws.slice(0, 30)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM