我有一个名为result的集合价值观:你知道吗
> db.result.findOne()
{
"_id" : ObjectId("53b05264421aa97e980ba404"),
"result" : [
{
"attempted" : 49,
"subject_total_marks" : 50,
"score" : 15,
"correct_subject_answer" : 15,
"subject" : "Biology"
},
{
"attempted" : 30,
"subject_total_marks" : 30,
"score" : 4,
"correct_subject_answer" : 4,
"subject" : "Chemistry"
},
{
"attempted" : 20,
"subject_total_marks" : 20,
"score" : 7,
"correct_subject_answer" : 7,
"subject" : "Physics"
},
{
"attempted" : 99,
"correct_subject_answer" : 26,
"score" : 26,
"subject_total_marks" : 100,
"subject" : "Total"
}
],
"useruid" : NumberLong(548),
"exam_code" : 301,
"ess_time" : 1404062120
}
现在我想根据结果数组中“total”的“score”对集合进行排序。然后根据物理,数学的分数排序,然后根据化学,最后是生物学。你知道吗
正如您可能已经尝试过的那样,不能将数组中的特定项指定为“键”,以便通过简单的查找进行“排序”。为此,您将需要aggregate方法来获取要排序的键。你知道吗
因此,在这里,在展开数组之后,您可以在^{} 语句中使用^{} 运算符“提取”匹配值。非规范化文档并不都具有相同的值,因为它们现在表示数组中的项,因此需要对它们进行测试。你知道吗
使用这些提取的键,您可以再次对整个文档进行排序,然后最终丢弃这些字段,因为您不再需要它们。你知道吗
相关问题 更多 >
编程相关推荐