MongoDB sort by only exists entry, key with value first and key with null or not exists last

Viewed 3293

I have a collection of 15000 documents. Some documents have sr_no with numeric values and other with absent of sr_no.

Now i want to get entries like all documents comes first which has sr_no with asc then all others.

I tried .find().sort({sr_no:1}) but it return all null entries first then asc with sr_no.

This question seems too close with duplicate. But slightly defer with numeric key.

I answered it with hack below.

1 Answers
Related