Sample Header Ad - 728x90

MongoDB Indexing - How many fields should I index?

0 votes
1 answer
726 views
I have a Collection in my db, called Post. Here's how it looks: Post { user: , school: , hashtag: , numberOfReports: , viewRanking: , type: , isPollOfTheDay: , createdAt: , endsAt: } (Obviously it also contains other, unrelated fields) So this Post collection, is being queried by 5 different screens in my app. **School**, **Hashtag**, **All Polls**, **Discover** & **Profile**. All queries are very similar to each other, but they differ. Let's have a look at them individually: > School Here, I have 2 queries 1. I compare by school.name (equal to), numberOfReports (less than), user.id (for blocks checking (not contained in)) and lastly, we sort in descending order either by createdAt or viewRanking (depends on the user) 2. I compare by isPollOfTheDay (equal to true) and endsAt (greater than or equal to) > Hashtag I compare by hashtag.id (equal to), numberOfReports (less than), user.id (for blocks checking (not contained in)) and lastly, we sort in descending order either by createdAt or viewRanking (depends on the user) > All Polls I compare by type (equal to), isPollOfTheDay (equal to false), numberOfReports (less than), user.id (for blocks checking (not contained in)) and lastly, we sort in descending order either by createdAt or viewRanking (depends on the user) > Discover I compare by school.name (not equal to), type (equal to), numberOfReports (less than), user.id (for blocks checking (not contained in)) and lastly, we sort in descending order by createdAt > Profile I compare by user.id (equal to) and we sort in descending order by createdAt These are all of my queries! I can say that they are called almost with the same frequency. My question is, should I just index all 9 fields? Are they too many to index? Should I ignore the isPollOfTheDay field, since it's a Boolean? (I've read that we shouldn't index Booleans) EDIT: Every document occupies about 200 bytes. We currently have 25K documents and growing in a pace of ~300/day. The only fields that can change, are viewRanking and numberOfReports where the first one will change often, whereas the second far less often! The selectivity of each query is high (I think), since the needed documents are found mainly by their first comparison. There are about 50 different school.names & another 50 hashtag.ids.
Asked by Sotiris Kaniras (151 rep)
Jan 9, 2019, 08:09 PM
Last activity: Jul 26, 2025, 05:09 PM