擅长:python、mysql、java
<blockquote>
<p>Storing these documents on an array before inserting would be a good idea? </p>
</blockquote>
<p>是的,很有可能。它减少了到数据库的往返次数。您应该监视您的系统,在插入时可能会因为IO等待而大量空闲(也就是说,开销和线程同步比实际的数据传输花费更多的时间)。</p>
<blockquote>
<p>How many documents should I store before inserting, then? </p>
</blockquote>
<p>这很难说,因为这取决于很多因素。经验法则:1000-10000。你得做点实验。在mongodb的旧版本中,整个批处理不能大于16MB的文档大小限制。</p>
<blockquote>
<p>Writing the jsons into a file and then using mongoimport would be faster?</p>
</blockquote>
<p>不,除非你的代码有缺陷。这意味着您必须复制数据两次,并且整个操作应该是IO绑定的。</p>
<p>另外,最好先添加所有文档,<em>然后</em>添加任何索引,而不是相反(因为这样每次插入都必须修复索引)</p>