I was surprised of the result of this request I found:
const counter = Order.findOne().sort({ order_number: -1 }).select("order_number");
I thought firstly it will find first one element and try to sort it by ‘order_number’ but It works very strange.
Firstly it will sort all collection by ‘order_number’ then it will find one element (the first) and select field ‘order_number’.
Before this I did it in 2 operations:
const result = await Order.find().sort({ order_number: -1 }).select("order_number"); // result [ 7, 4, 2, 1]
const counter = result[0];
Why it works in such sequense ?