Mongo error 13431 have to have sort key in projection and removing it

Error: When using shard collection and querying, if we explicitly want to omit a field but want to sort on that field, Mongo raises error code 13431 since mongos can’t sort without that missing field:

db.collection.find({},{_id:0}).sort({_id:1});

error: {
“$err” : “have to have sort key in projection and removing it”,
“code” : 13431
}

Solution: do not omit the field used in sort

Mongo error 13431 have to have sort key in projection and removing it