问题
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:
I used a dirty hack for this.
MongoDB doc says that they have priorities for sorting as posted below image.
So when i sort with asc then it sort first all null (empty key consider as null) entries then sort numeric entries.
What is hack here ?
Store sr_no : ""
with empty string default.
Now it will sort first numeric values then string.
来源:https://stackoverflow.com/questions/36028267/mongodb-sort-by-only-exists-entry-key-with-value-first-and-key-with-null-or-not