pipeline = [{'$project': {'category': 1, 'name': 1, 'w': {'$cond': [{'$eq':
    ['$category', 'B']}, 1, {'$cond': [{'$eq': ['$category', 'C']}, 2, 3]}]
    }}}, {'$sort': {'w': 1}}]
pprint.pprint(list(collection.aggregate(pipeline=pipeline)))
python - sort mongodb by the value of one key