increment counter in node js mongodb
<p>I want to make a counter of how many time the server js file has started, for my website using mongodb driver for angular js.</p>

<p>I want to save a varible named counter which has a value of 0 and then increment that value each time that the server is running. my code is below. as you can see my code doesn't acutally update the field in the db. just the varible.</p>

<p>beside that... well.. the whole code I wrote seems like bad practise. I basically have a document with {id:&lt;>,count:0} and I am looping through all the count fields which are greater the -1 (i.e. integers) although I have only got just 1 count field.</p>

<p>isn't there any simple way to persist/get this 1 value from the db?</p>

<p>How can I update the field inside the db itself using something like $inc, in the easiest way possible?</p>

<p>Thanks</p>

<pre><code>MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    if (err) {
        console.log(err);
    }
    else {
        console.log("Connected correctly to DB.");
        var dbusers =db.collection('users');
        var cursor =dbusers.find( { "count": { $gt: -1 } } );
        cursor.each(function(err, doc) {
            assert.equal(err, null);
            if (doc != null) {
                doc.count=doc.count+1;
            }  
        }
        );
    }
    db.close();
});
</code></pre>