Counter counter in node js mongodb

I want to make a counter on how much time the server js file has been running for my site using the mongodb driver for angular js.

I want to save a variable named counter that has a value of 0, and then increment that value every time the server is running. my code is below. as you can see my code does not update the field in db. just a species.

next to this ... well .. all the code I wrote seems to be bad. I basically have a document with {id: <>, count: 0}, and I look through all the count fields that are greater than -1 (that is, Integers), although I only have 1 count field.

Is there no easy way to save / get this value from db?

How can I update a field inside db itself using something like $ inc, in the easiest way?

thank

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();
});
+4
1

:

MongoClient.connect(url, function(err, db) {
    if (err) {
        console.log(err);
        return db.close();
    }

    console.log("Connected correctly to DB.");
    // update a record in the collection
    db.users.update(
        // find record with name "MyServer"
        { name: "MyServer" },
        // increment it property called "ran" by 1
        { $inc: { ran: 1 } }
    );
    return db.close();
});

, . , - :

  • , "count" -1

  • 1

  • .

, , - 3. , . , , - .

- . - .

+2

Source: https://habr.com/ru/post/1648707/


All Articles