A
A
Andrey Puzov2015-08-31 13:32:02
MongoDB
Andrey Puzov, 2015-08-31 13:32:02

Why is mongoDB not executing the query again?

mongo.connect(config.get('mongodb:uri') + config.get('mongodb:dbName'), function(err, db) {
        if (err) throw err;
        var col = db.collection(config.get('mongodb:history'));
        var mess = {};
        col.find({_id: {$gt: midnight}}).forEach(function (res) {
            if(res.priv.length == 0 && res.confirm.length == 0) {
                mess = res.whoSend + ': ' + res.message;
                arrayOfMessages.push(mess);
            };
        });
    });
    return arrayOfMessages;

Good afternoon.
This function returns an array of today's messages.
If you log in, write a message, re-login (under any nickname), then the last written messages will not be visible until you restart the server.
It is necessary to somehow close the connection, so that when he logs in, he connects again and sees new ones?
Or what should be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Puzov, 2015-08-31
@puzzlo

// showTodaySimpleMessages.js 

var   config = require('config')
    , mongo = require('mongodb').MongoClient;

var showTodaySimpleMessages = function () {

    var arrayOfMessages = [];
    var now = new Date();
    var midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime().toString();

    mongo.connect(config.get('mongodb:uri') + config.get('mongodb:dbName'), function(err, db) {
        if (err) throw err;
        var col = db.collection(config.get('mongodb:history'));
        var mess = {};
        col.find({_id: {$gt: midnight}}).forEach(function (res) {
            if(res.priv.length == 0 && res.confirm.length == 0) {
                mess = res.whoSend + ': ' + res.message;
                arrayOfMessages.push(mess);
            };
            db.close();
        });
    });
    return arrayOfMessages;
};
module.exports = showTodaySimpleMessages();

connected, found those messages that were written today ( _id - the time the message was created in milliseconds ) . if not private and not for confirmation - add to the array. returned an array.
on the server, all messages from the array were emitted to the person who entered.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question