我有一组学生,每个学生都有一个类似以下样子的记录,我想按照分数score
降序排序scores
数组。
在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
}
]
}
我正在尝试这个咒语....
doc = db.students.find()
for (_id,score) in doc.scores:
print _id,score
但是它没有起作用。
db.students.updateMany({}, {'$push':{'scores':{'$each':[], '$sort':{'score': 1}}}})
,参见mongodb文档。 - Treefish Zhang