I have a mongoDB
containing millions of documents.
I want to query the database and sort the results naturally using a specific field.
From Mongo documentation it seems that only naive sort supported. Is there a way to sort naturally?
Example:
- doc 1 _id: "abc10.def"
- doc 2 _id: "abc2.def"
- doc 3 _id: "abc1.def"
Query:
db_collection.find({}).sort({_id: 1})
returns a list by order:
- doc3 ("abc1.def")
- doc1 ("abc10.def")
- doc2 ("abc2.def")
how can I sort to receive the following list?:
- doc3 ("abc1.def")
- doc2 ("abc2.def")
- doc1 ("abc10.def")
$natural
returns the documents in the order in which the database refers to them on disk. Please read the question and decide if that's really what OP needs – mickl