Database

[{"code":"S-444"},{"code":"S-123"},{"code":"A-123"},{"code":"S-515"},{"code":"A-515"}]

Query

db.collection.aggregate([{$addFields:{customSortField:{$cond:[{"$regexMatch":{"input":"$code","regex":"^S-"}},0,1]}}},{$sort:{customSortField:1,code:1}},{$unset:"customSortField"}])

Result