Database
[{"_id":ObjectId("6451529ad254d565bd12156b"),"contentType":"Json","fieldName":"xxx","method":"GET","value":"Car","location":"header","code":404},{"_id":ObjectId("6451529ad254d565bd12156d"),"contentType":"Json","fieldName":"xxx","method":"GET","value":"Plane","location":"header","code":404},{"_id":ObjectId("6451529ad254d565bd121570"),"contentType":"Json","fieldName":"yyy","method":"GET","value":"Smile","location":"header","code":404},{"_id":ObjectId("6451529ad254d565bd13156b"),"contentType":"Empty","fieldName":"zzz","method":"POST","value":"Train","location":"header","code":200},{"_id":ObjectId("6451529ad254d5651112156a"),"contentType":"Empty","fieldName":"zzz","method":"POST","value":"Player","location":"body","code":200},{"_id":ObjectId("6451529ad254d565bd12166d"),"contentType":"Empty","fieldName":"zzz","method":"POST","value":"Van","location":"body","code":404},{"_id":ObjectId("6451529ad254d565bd121670"),"contentType":"Json","fieldName":"name4","method":"GET","value":"Van","location":"body","code":200}]
Query
db.collection.aggregate([{"$match":{"contentType":"Json","code":{"$in":[200,404]}}},{"$group":{"_id":{"code":"$code","field":"$fieldName","location":"$location"},"values":{"$push":"$value"}}},{"$group":{"_id":"$_id.code","samples":{"$push":{"field":"$_id.field","location":"$_id.location","values":{"$slice":["$values",5]}}}}},{"$group":{"_id":null,"data":{"$push":{"code":"$_id","samples":"$samples"}}}},{"$unset":"_id"}])