如何按值mongodb排序对象字段

时间:2017-05-05 15:54:14

标签: node.js mongodb

考虑以下结构:

"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
    }
}

labels个收藏品下, 和以下代码:

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

如何根据酒店价值对酒店进行排序?

1 个答案:

答案 0 :(得分:0)

如果您正在寻找特定酒店的分类,您可以使用.sort({"ranks.hotel_1":1})