相关疑难解决方法(0)

如何在mongoDB中对集合记录中的数组进行排序

MongoDB noob在这里......

好的,我有一个学生集合,每个都有一个如下所示的记录....我想按照降序排序'类型':'家庭作业'分数.

那个咒语在mongo shell上是什么样的?

> db.students.find({'_id': 1}).pretty()
{
        "_id" : 1,
        "name" : "Aurelia Menendez",
        "scores" : [
                {
                        "type" : "exam",
                        "score" : 60.06045071030959
                },
                {
                        "type" : "quiz",
                        "score" : 52.79790691903873
                },
                {
                        "type" : "homework",
                        "score" : 71.76133439165544
                },
                {
                        "type" : "homework",
                        "score" : 34.85718117893772
                }
        ]
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试这个咒语....

 doc = db.students.find()

 for (_id,score) in doc.scores:
     print _id,score
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

javascript arrays sorting mongodb

29
推荐指数
4
解决办法
5万
查看次数

标签 统计

arrays ×1

javascript ×1

mongodb ×1

sorting ×1