AI Briefing
KO

How We Solved a MongoDB Atlas Search Sorting Issue

·2022.11.16 22:00

Key point

This covers the cause of and solution to performance degradation that occurs when using $search and $sort together in MongoDB Atlas Search.

1 / 2

Details

Inflearn adopted Atlas Search instead of the existing RDB's LIKE operation to improve search quality. However, a problem arose where query speed dropped sharply when applying sorting based on specific fields, such as by latest or by likes.

The cause of this problem is that the data filtering process (mongot) and the sorting process (mongod) are separated. A bottleneck occurs in the process where mongod individually looks up and sorts the numerous IDs returned as $search results.

The first solution to address this is to use the storedSource and returnStoredSource options. By including the fields needed for sorting in the search index in advance, mongod can perform sorting immediately without a separate collection lookup.

However, since the index size can grow larger, it is recommended to include only the minimum necessary fields. If additional data is needed, it can be handled efficiently by combining a $lookup stage.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.