0

ここにMongoDB初心者...

だから、私はこのようなコレクション内の最小値スコアを出力しようとしています...

        > 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
                        }
                ]
        }

私が使っている呪文はこんな感じです...

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'
    }},

    // grab the minimum value score
    { $match: {
        'scores.min.score': 1
    }}
)

私が得ている出力はこれです...

{ "result" : [ ], "ok" : 1 }

私は何を間違っていますか?

4

2 に答える 2

0
> db.students.aggregate(     
{ $unwind:  "$scores" },`
{ $match:{"scores.type":"homework"} }, 
{ $group: { 
    _id : "$_id", 
   maxScore : { $max : "$scores.score"}, 
   minScore: { $min:"$scores.score"} 
  }
});

mongoDB のコレクション内の各アイテムを集計する方法

于 2012-11-21T00:01:17.430 に答える
0

あなたは正しい考えを持っていますが、集計の最後のステップでやりたいことは、学生ごとにすべてのスコアをグループ化し、$min 値を見つけることです。

最後のパイプライン操作を次のように変更します。

{ $group: {
        _id: "$_id",
        minScore: {$min: "$scores.score"}
    }}
于 2012-11-20T00:10:47.947 に答える