Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Usually, <code>$in</code> queries are surprisingly fast, but whether it's the right approach depends on the cardinality of the data (max. events per user, max. users per event) and your query patterns.</p> <p>In general, the idea of indexes is to avoid having to link back-and-forth. That makes the cumbersome updates you mentioned unnecessary. It's also easier to query, easier to maintain, and is easier to paginate.</p> <p>The last argument is particularly important but depends on your query patterns: Let's say you want to show the ten most recent events a user attended to. Then you can create an index <code>{userIds : 1, eventDate: -1}</code> which will perfectly match the query and doesn't have to pull or iterate <em>all</em> the events that the user went to.</p> <p>If you wanted to do this query using the other method, you'd also have to store the <code>eventDate</code> in the user, which seems rather awkward.</p> <p>On the other hand, if events are huge, you could run into problems with the object size of the event (think 1 million participants). You might want to denormalize the names of participants for display purposes, which makes the object even larger.</p> <p>If you choose to work with <code>$in</code> queries, keep in mind that</p> <ol> <li>performance degrades as the array becomes large. I'm not sure what causes this, but I had trouble when the array grew past a one or two thousand(!) elements.</li> <li>If the ids spread very far, MongoDB might have to hit a lot of buckets. This greatly depends on the type of keys you're using, but it can be painful (e.g. if you're using <code>ObjectIds</code> and the user attended an event every day you'll probably hit every <code>_id</code> bucket you have, also the older ones, which can be expensive).</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload