One option is to do custom sort by aggregate cond
Mongo query is
db.products.aggregate([
{"$project":{
"sortField":
{"$cond":[{"$eq":["$status", "IN_PROGRESS"]}, 1,
{"$cond":[{"$eq":["$status", "ON_DELIVERY"]}, 2,
3]} ]},
"status": true
}},
{"$sort":{"sortField": 1}}
]);
For collection data
db.products.insertMany([{"sku": 1, "status": "IN_PROGRESS"}, {"sku": 2, "status": "DELIVERED"}, {"sku": 3, "status": "ON_DELIVERY"}]);
Output
[
{
"sortField": 1,
"status": "IN_PROGRESS"
},
{
"sortField": 2,
"status": "ON_DELIVERY"
},
{
"sortField": 3,
"status": "DELIVERED"
}
]
And for golang mongo driver
pipeline := []bson.M{
{"$project": bson.M{"sortField":
bson.M{"$cond": bson.A{bson.M{"$eq": bson.A{"$status", "IN_PROGRESS"}}, 1,
bson.M{"$cond": bson.A{ bson.M{"$eq": bson.A{"$status", "ON_DELIVERY"}}, 2, 3}} }},
"status": true}},
{"$sort": bson.M{"sortField": 1}},
}
model.Collection..Aggregate(context.Background(), pipeline)