潇湘沐
在 mongo shell 中运行以下管道应该会给你正确的结果:pipeline = [ { "$group": { "_id": { "year": { "$year": "$timestamp" }, "dayOfYear": { "$dayOfYear": "$timestamp" }, "minute_interval": { "$subtract": [ { "$minute": "$timestamp" }, { "$mod": [{ "$minute": "$timestamp" }, 15] } ] } }, "averageSpeed": { "$avg": "$speed" } } }]db.collection.aggregate(pipeline)其中等效的 mGo 表达式如下(未经测试):pipeline := []bson.D{ bson.M{ "$group": bson.M{ "_id": bson.M{ "year": bson.M{ "$year": "$timestamp" }, "dayOfYear": bson.M{ "$dayOfYear": "$timestamp" }, "minute_interval": bson.M{ "$subtract": []interface{}{ bson.M{ "$minute": "$timestamp" }, bson.M{ "$mod": []interface{}{ bson.M{ "$minute": "$timestamp" }, 15, }, } } } } }, "averageSpeed": bson.M{ "$avg": "$speed" } } }}pipe := collection.Pipe(pipeline)iter := pipe.Iter()