聚合管道后获取id值

时间:2018-01-22 18:13:47

标签: mongodb mongodb-query aggregation-framework mongo-shell

考虑在名为sample

的集合中跟踪数据
{ "_id" : 1, "student_id" : 10, "type" : "homework", "score" : 63 },
{ "_id" : 3, "student_id" : 10, "type" : "homework", "score" : 14 },
{ "_id" : 2, "student_id" : 10, "type" : "quiz", "score" : 31 },
{ "_id" : 4, "student_id" : 10, "type" : "quiz", "score" : 54 },
{ "_id" : 5, "student_id" : 11, "type" : "homework", "score" : 33 },
{ "_id" : 7, "student_id" : 11, "type" : "homework", "score" : 74 },
{ "_id" : 6, "student_id" : 11, "type" : "quiz", "score" : 51 },
{ "_id" : 8, "student_id" : 11, "type" : "quiz", "score" : 24 }

我希望获得每个学生在家庭作业类型中获得的最低分数_id。

我有以下查询

db.sample.aggregate([
    {   $match: {   type: 'homework' }, },
    {
        $group: {
            _id: {
                student_id: '$student_id',
            },
            mark: {
                $min: '$score',
            }
        }
    }
])

计算最小标记,但我想要相应的_ids。

上述查询的结果是

{ "_id" : { "student_id" : 11 }, "mark" : 33 }
{ "_id" : { "student_id" : 10 }, "mark" : 14 }

获取ID是否正确。

1 个答案:

答案 0 :(得分:1)

var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var myObj = JSON.parse(this.responseText); document.getElementById("luno").innerHTML = myObj.bid; } }; xmlhttp.open("GET", "https://api.mybitx.com/api/1/ticker?pair=XBTZAR", true); xmlhttp.send();$first升序而不是$sort一起使用,以最低得分拉入整个文档($min)并映射必填字段。

$$ROOT