从nodejs获取最后更新的mongo id

时间:2014-07-16 09:30:23

标签: node.js mongodb socket.io

正在使用mongodb和nodejs处理推送通知。 我可以在浏览器中看到新添加的通知(在Mongodb中是addede) 但是,如果我更新了记录,则该值不会在浏览器中更新

// if no error get reference to colelction named: 'notifications'
    db.collection('notifications', function(err, collection){
        if(err) {
            throw err;
        }

        // if no error apply a find() and get reference to doc
        collection.find().sort({
            $natural: -1
        }).limit(1).nextObject(function(err, doc) {
            // Rewind the cursor, resetting it to point to the start of the query
            if(err) {
                throw err;
            }
            //  using tailable cursor get reference to our very first doc
            var query = {
                _id: {
                    $gt: doc._id
                }
            };
            var options = {
                tailable: true, 
                awaitdata: true, 
                numberOfRetries: -1
            };
            var cursor = collection.find(query, options).sort({
                $natural: 1
            });

            // This function will take cursor to next doc from current as soon as 'notifications' database is updated
            function next() {
                cursor.nextObject(function(err, message) {
                    if (err) throw err;
                    console.log(message.message);
                    mdsok.volatile.emit('notification', message);
                    next();
                });
            }
            // what you need to do is: call it first time
            next();
        });

这就是我在代码中所做的事情。

当我在db中更新相同内容时,我该怎么做才能更新浏览器中的值。

请帮帮我。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我的问题在某种程度上得到了解决。

var http = require('http'),
fs = require('fs'),
// NEVER use a Sync function except at start-up!
index = fs.readFileSync('index.html');

// Send index.html to all requests
var app = http.createServer(function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'text/html'
    });
    res.end(index);
});

// Socket.io server listens to our app
var io = require('socket.io').listen(app);
var MongoClient = require('mongodb').MongoClient;

function getdata(){
    MongoClient.connect("mongodb://127.0.0.1:27017/test", function(err, db) {
        var collection = db.collection('my_collection');

        var stream = collection.find({
            //'_id': new ObjectID('53eb6f2e75fd7ad00d000029')
            //_id: ObjectID.createFromHexString("53eb6f2e75fd7ad00d000029")
            }).stream();
        stream.on("data", function(item) {
            io.sockets.emit('db_status', {
                status: item.status
            });
            prev = item.status;
            console.log(prev);
        });
        stream.on("end", function() {
            console.log("Done loading data");
        });
    });
}


// Send current time every 5 secs
setInterval(getdata, 5000);

// Emit welcome message on connection
io.sockets.on('connection', function(socket) {

    socket.emit('welcome', {
        message: 'Welcome!'
    });            

    socket.on('i am client',function(data){
        console.log(data);
    });
});


app.listen(3000);

每5秒,我正在点击数据库并获取值并在浏览器中显示它。

要获取新插入的对象,我们在.nextObject()

中使用node.js

有没有办法在node.js中获取上述数据库的更新对象。