the*_*nso 29 javascript arrays sorting 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)
但它不起作用.
Ste*_*nie 47
您需要在应用程序代码中操作嵌入式数组,或在MongoDB 2.2中使用新的聚合框架.
mongo
shell中的示例聚合:
db.students.aggregate(
// Initial document match (uses index, if a suitable one is available)
{ $match: {
_id : 1
}},
// Expand the scores array into a stream of documents
{ $unwind: '$scores' },
// Filter to 'homework' scores
{ $match: {
'scores.type': 'homework'
}},
// Sort in descending order
{ $sort: {
'scores.score': -1
}}
)
Run Code Online (Sandbox Code Playgroud)
样本输出:
{
"result" : [
{
"_id" : 1,
"name" : "Aurelia Menendez",
"scores" : {
"type" : "homework",
"score" : 71.76133439165544
}
},
{
"_id" : 1,
"name" : "Aurelia Menendez",
"scores" : {
"type" : "homework",
"score" : 34.85718117893772
}
}
],
"ok" : 1
}
Run Code Online (Sandbox Code Playgroud)
Xav*_*hot 17
从 开始Mongo 5.2
,这就是新$sortArray
聚合运算符的确切用例:
// {
// name: "Aurelia Menendez",
// scores: [
// { type: "exam", score: 60.06 }
// { type: "quiz", score: 52.79 }
// { type: "homework", score: 71.76 }
// { type: "homework", score: 34.85 }
// ]
// }
db.collection.aggregate([
{ $set: {
scores: {
$sortArray: {
input: "$scores",
sortBy: { score: -1 }
}
}
}}
])
// {
// name: "Aurelia Menendez",
// scores: [
// { type: "homework", score: 71.76 },
// { type: "exam", score: 60.06 },
// { type: "quiz", score: 52.79 },
// { type: "homework", score: 34.85 }
// ]
// }
Run Code Online (Sandbox Code Playgroud)
这:
$sortArray
)scores
数组进行排序 ( input: "$scores"
)score
通过对s ( sortBy: { score: -1 }
)进行排序$unwind
,$sort
和$group
阶段的组合由于可以通过不同方式管理这个问题,我想说另一种解决方案是“插入和排序”,这样您将在进行 Find() 时获得 Ordered 数组。
考虑这个数据:
{
"_id" : 5,
"quizzes" : [
{ "wk": 1, "score" : 10 },
{ "wk": 2, "score" : 8 },
{ "wk": 3, "score" : 5 },
{ "wk": 4, "score" : 6 }
]
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们将更新文档,进行排序。
db.students.update(
{ _id: 5 },
{
$push: {
quizzes: {
$each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
$sort: { score: -1 },
$slice: 3 // keep the first 3 values
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
结果是:
{
"_id" : 5,
"quizzes" : [
{ "wk" : 1, "score" : 10 },
{ "wk" : 2, "score" : 8 },
{ "wk" : 5, "score" : 8 }
]
}
Run Code Online (Sandbox Code Playgroud)
文档:https : //docs.mongodb.com/manual/reference/operator/update/sort/#up._S_sort
这就是我们用JS和mongo控制台解决这个问题的方法:
db.students.find({"scores.type": "homework"}).forEach(
function(s){
var sortedScores = s.scores.sort(
function(a, b){
return a.score<b.score && a.type=="homework";
}
);
var lowestHomeworkScore = sortedScores[sortedScores.length-1].score;
db.students.update({_id: s._id},{$pull: {scores: {score: lowestHomeworkScore}}}, {multi: true});
})
Run Code Online (Sandbox Code Playgroud)