Database
[{"products":[{"name":"name1","ids":[4,5,6]}],"author":"Dante","shelf":"a"},{"products":[{"name":"name1","ids":[4,5,]}],"author":"Homer","shelf":"a"},{"products":[{"name":"name1","ids":[2]}],"author":"Dante","shelf":"b"},]
Query
db.collection.aggregate([{"$match":{"shelf":"a"}},{"$set":{"rank":{"$eq":[{"$filter":{"input":"$products","cond":{"$in":[6,"$$this.ids"]}}},[]]}}},{"$sort":{"rank":-1,"author":1}},{"$project":{"rank":0}}])