Database
[{"collection":"collection","count":100,"content":{"_id":{"type":"autoincrement","autoType":"int"},"res":{"type":"enum","values":[10,15,28,35,36,36,37,38,39,39,39,40,40,40,41,41,41,41,41,42,42,43,43,44,44,44,46,46,46,46,47,47,47,47,47,47,47,47,48,48,48,49,49,49,49,49,50,51,51,52,52,52,53,53,53,53,53,54,54,55,55,56,56,56,56,56,56,57,57,57,57,58,58,58,58,59,59,59,60,60,60,60,60,61,61,61,63,64,65,65,66,66,67,67,67,73,73,85,90,95]}}}]
Query
db.collection.aggregate([{/** partition res into quartiles*/"$bucketAuto":{"groupBy":"$res","buckets":4}},{/** get the max of each quartile*/"$group":{"_id":"$_id.max"}},{/** sort the qusrtile maxs*/"$sort":{"_id":1}},{/** put sorted quartile maxs into array*/"$group":{"_id":null,"maxs":{"$push":"$_id"}}},{/** assign Q1 and Q3*/"$project":{"_id":0,"q1":{"$arrayElemAt":["$maxs",0]},"q3":{"$arrayElemAt":["$maxs",2]}}},{/** set IQR*/"$set":{"iqr":{"$subtract":["$q3","$q1"]}}},{/** assign upper/lower outlier thresholds*/"$project":{"outlierThresholdLower":{"$subtract":["$q1",{"$multiply":["$iqr",1.5]}]},"outlierThresholdUpper":{"$add":["$q3",{"$multiply":["$iqr",1.5]}]}}},{/** get outlier _id's*/"$lookup":{"from":"collection","as":"outliers","let":{"oTL":"$outlierThresholdLower","oTU":"$outlierThresholdUpper"},"pipeline":[{"$match":{"$expr":{"$or":[{"$lt":["$res","$$oTL"]},{"$gt":["$res","$$oTU"]}]}}},{"$project":{"_id":1}}]}}])