Friday 5 May 2017

How to order object fields by value mongodb

Considering the following structure:

"label_5" : {
    "ranks" : {
        "hotel_1" : 0.738805199017347,
        "hotel_2" : 0.17653657117268828,
        "hotel_3" : 0.04071552339502227,
        "hotel_4" : 0.12543627348684705,
        "hotel_5" : 0.375692808082533,
        "hotel_6" : 0.6796857473988642,
        "hotel_7" : 0.18080608668170495
    }
},
"label_6" : {
    "ranks" : {
        "hotel_1" : 0.40339830434450485,
        "hotel_2" : 0.7106559576870051,
        "hotel_3" : 0.6349776144488348,
        "hotel_4" : 0.4260347490574752,
        "hotel_5" : 0.6244178232434163,
        "hotel_6" : 0.6569190693595632,
        "hotel_7" : 0.16378308796300156,
        "hotel_8" : 0.16766423698668875,
        "hotel_9" : 0.7319011899938765
    }
},
"label_7" : {
    "ranks" : {
        "hotel_1" : 0.33694055774124787,
        "hotel_2" : 0.4293637949091027,
        "hotel_3" : 0.7925947261514583,
        "hotel_4" : 0.8867890134535386,
        "hotel_5" : 0.7927268526887514,
        "hotel_6" : 0.09449794446136539
    }
}

under labels collections, and the following code:

db.collection("labels").find({},{label_6:1, label_5:1})

How can I sort the hotels by their value?



via Ben

No comments:

Post a Comment