Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What happens if you explicitly return a value at the end of the db.open callback function? Your generated javascript code is pushing all of your collection.insert returns onto a big "_results" array, which would get slower and slower, I imagine.</p> <pre><code>db.open(function(error, client) { var collection, doc, hash, i, key, _i, _results; if (error != null) { throw error; } collection = mongodb.Collection(client, "foo"); _results = []; for (i = _i = 0; 0 &lt;= times ? _i &lt; times : _i &gt; times; i = 0 &lt;= times ? ++_i : --_i) { ... _results.push(collection.insert(doc, { safe: true }, function(error, response) { if (error) { return console.log(error.message); } })); } return _results; }); </code></pre> <p>Try adding this at the end of your coffeescript:</p> <pre><code> collection.insert doc, safe: true, (error, response) -&gt; console.log error.message if error return </code></pre> <p>*<em>Update: *</em> So, I actually tried to run your program, and noticed a few more issues:</p> <p>The biggest problem is you're trying to spawn a million inserts in a synchronous fashion, which will really kill your RAM, and eventually stop inserting (at least, it did for me). I killed it at 800MB RAM or so. </p> <p>You need to change the way you're calling collection.insert() so that it works asynchronously.</p> <p>I rewrote it like so, breaking out a couple of functions for clarity:</p> <pre><code>mongodb = require "mongodb" microtime = require "microtime" crypto = require "crypto" gen = () -&gt; hash = crypto.createHash "sha1" hash.update "" + microtime.now() + (Math.random() * 255 | 0) key = hash.digest "hex" key: key, foo1: 1000, foo2: 1000, foo3: 1000, bar1: 2000, bar2: 2000, bar3: 2000, baz1: 3000, baz2: 3000, baz3: 3000 times = 1000000 i = times insertDocs = (collection) -&gt; collection.insert gen(), {safe:true}, () -&gt; console.log "Inserting #{times-i}..." if i % 100000 == 0 if --i &gt; 0 insertDocs(collection) else process.exit 0 return server = new mongodb.Server "127.0.0.1", 27017 db = mongodb.Db "test", server db.open (error, db) -&gt; throw error if error? db.collection "foo", (err, collection) -&gt; insertDocs(collection) return return </code></pre> <p>Which finished in ~3 minutes:</p> <pre><code>wfreeman$ time coffee mongotest.coffee Inserting 0... Inserting 100000... Inserting 200000... Inserting 300000... Inserting 400000... Inserting 500000... Inserting 600000... Inserting 700000... Inserting 800000... Inserting 900000... real 3m31.991s user 1m55.211s sys 0m23.420s </code></pre> <p>Also, it has the side benefit of using &lt;100MB of RAM, 70% CPU on node, and 40% CPU on mongod (on a 2 core box, so it looks like it wasn't maxing out the CPU).</p>
 

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