Database
[{"name":"somename","level":"123","nested":[{"somefield":"test3","file":{level:"123"}},{"somefield":"test2","file":{level:"124"}},{"somefield":"test","file":{level:"123"}}]}]
Query
db.collection.aggregate([{$unwind:"$nested"},{$match:{$expr:{$eq:["$nested.file.level","$level"]}}},{$set:{min:{$switch:{branches:[{"case":{$eq:["$nested.somefield","test"]},then:1},{"case":{$eq:["$nested.somefield","test2"]},then:2},{"case":{$eq:["$nested.somefield","test3"]},then:3},{"case":{$eq:["$nested.somefield","test4"]},then:4},{"case":{$eq:["$nested.somefield","test5"]},then:5}],"default":100}}}},{$sort:{min:1}},{$limit:1},{$group:{_id:"$_id",setting:{$first:{$cond:{if:{$lte:["$min",5]},then:"$nested",else:"Did not match"}}}}}])