title: What is the limit of HABTM relations in MongoDB / MongoId?
body: <p>When using a <a href="https://docs.mongodb.com/mongoid/current/tutorials/mongoid-relations/#has-and-belongs-to-many" rel="nofollow noreferrer"><code>has_and_belongs_to_many</code> with MongoId</a>, like:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class User
    include Mongoid::Document
    has_and_belongs_to_many :user_teams
  end
  </code></pre>
  
  <p>Both models get a new field which holds the ids of the related objects:</p>
  
  <pre><code>user.user_team_ids = [ ... ]
  </code></pre>
  
  <p>My Question: With the <a href="https://docs.mongodb.com/manual/reference/limits/#BSON-Document-Size" rel="nofollow noreferrer">limitations of documents, which is 16MB</a>, is it correct to assume the limit is around 450.000 relations per object? </p>
  
  <p>My absolute naive way of getting this number is:</p>
  
  <pre><code>16 MB / 37 byte per ID = 432.432 ID
  </code></pre>
  
  <p><em>(37 byte is the length of an <code>JSON</code> string of an <code>BSON::ObjectId</code>)</em></p>
  
  <p>This is not about if you should use relations with MongoDB or not - it's more a question to understand the implications of using them.</p>