How to Auto sort on one field in MongoDB?

I am new to the MongoDB database. Because the number of records is very large, it takes us a lot of time to sort them in each query. I want to have a field called Id, like in SQL server, that I can set myself. And when I retrieve the information from this table, the information will be returned to me sorted according to the Id field. With my research, I found out that fields cannot be auto-incremented in NoSQL databases. Now if we index on the Id field according to the following command:

db.users.createIndex({ Id: 1 })

When reading the information in this table, the records will be sorted by Id or do we need to re-sort in the query?

Hey @Hossein_Mahdavi,

Welcome to the MongoDB Community!

Based on the following documentation, if you index the Id field, the records will be sorted by Id when you read the documents from the collection, and there is no need to re-sort the query.

However, if you want to sort the documents by Id in descending order, you can use the following query:

db.users.find().sort({ Id: -1 })

The -1 in the sort() method tells MongoDB to sort the documents in descending order.

Let us know if you have any other questions.

Regards,
Kushagra

I think this answer is a bit misleading.

I am pretty sure that despite the existence of index {Id:1} that the documents are not necessarily return in sorted order of Id unless sort({Id:1}) is used. Calling db.users.find() will produce document in a random order. Calling db.users.find().sort({Id:1}) will present the document in ascending order of Id, a sort will not be perform because the index can be used to returned the documents in the desired order.

By reading myself I am not that clear either but to resume.

1 - db.users.find() returns documents in random order
2 - db.users.find().sort({Id:1}) returns document sorted by Id but the server do not perform a sort
3 - db.users.find().sort({Id:-1}) returns document sorted descending but the server do not perform a sort
4 - db.users.find().sort({FieldWithourIndex:1}) returns document sorted by FieldWithoutIndex but the server performs a sort because there is no index.

Conclusion:

  • if you want documents to be in a specific order, you must sort
  • if you sort and want to be fast, you must create an index
3 Likes

Hey @Hossein_Mahdavi,

As @steevej rightly pointed out, it’s worth considering the following scenarios:

  1. db.users.find() might return documents in somewhat random order. If you require a specific order, it’s recommended to use explicit sorting.
  2. Using db.users.find().sort({ Id: 1 }) should give you the desired result of documents sorted in ascending order of "Id".
    However, it’s important to note that the index in this scenario can potentially fulfill the sorting requirement so that the server will not have to perform additional sort operations also known as in-memory sort. To read more please refer to the Use Indexes to Sort Query Results documentation.

Feel free to experiment with different queries to see how they behave in your specific use case. If you’re aiming for both specific ordering and fast retrieval, a combination of indexing and sorting is recommended.

If you have further questions or need clarification, don’t hesitate to ask.

Best regards,
Kushagra

3 Likes

Thank you @steevej for your response. That is the perfect answer.

1 Like