使用Node.js监视Mongo的更改

时间:2020-11-27 00:06:45

I'm using Node.js for some project work and I would like to monitor my Mongo database (collection) for changes, basically fire an event if something gets added.

我使用的节点。一些项目工作的js,我想要监控我的Mongo数据库(收集)的变化,如果有什么东西被添加进来,基本上就会触发一个事件。

Anyone know if this is possible? I'm using the node-mongodb-native drivers.

有人知道这是否可能吗?我使用的是node-mongodb本地驱动程序。

If it's not I'd also like any available pointers on pushing data from the server (run with node) to the client browser.

如果不是,我还希望任何可用的指针将数据从服务器(与节点一起运行)推到客户端浏览器。

4 个解决方案

#1


3  

The question is if all data is added to your database through your node.js app. If so, you can use the EventEmitter class of node.js to trigger an event (http://nodejs.org/api.html#eventemitter-14).

问题是是否所有数据都通过节点添加到数据库中。如果是,可以使用node的EventEmitter类。触发事件(http://nodejs.org/api.html#eventemitter-14)。

If the database is populated by some other app, things are getting difficult. In this case you would need something like a database trigger, which is AFAIK not yet availabled in MongoDB.

如果数据库被其他应用程序填充,事情就会变得越来越困难。在这种情况下,您将需要类似于数据库触发器的东西,这是在MongoDB中还没有使用的AFAIK。

Pushing Events to the Client (aka Comet) will be possible once the HTML 5 websockets API makes its way into all major browsers.

一旦HTML 5 websockets API进入所有主流浏览器,将事件推送到客户端(又名Comet)将成为可能。

In the meantime, you can only try to emulate this behaviour using techniques like (long-term) AJAX polling, forever frame etc. but each of them has its weaknesses.

与此同时,您只能尝试使用诸如(长期的)AJAX轮询、永远框架等技术来模拟这种行为,但每种方法都有其弱点。

#2


3  

I would turn on replication in your mongodb. There is a replicate? database that contains a list of changes, similar to the mysql replication log. You can monitor that.

我将在mongodb中打开复制。有一个复制?包含更改列表的数据库,类似于mysql复制日志。您可以监视。

-daniel

丹尼尔

#3


1  

Almost 3y since the last answer. I would suggest looking at:

从上次回答到现在已经快3年了。我的建议是:

npm install mubsub should get you there

npm安装mubsub会让你到达那里

#4


0  

collection.insert({"key1":val1,"key2":"val2"}, function(err, info){
if(err){ //handle this } else{ if(info){

收集。插入({“key1”:val1,“key2”:“val2”},函数(err, info){if(err){// /处理此}else{if(info)} {

you call a fireandforgetfunction(info); here that can write to logs or send to SQS or do some other child spawn or in process thing. This could even be a callback but I think a fire and forget may do in most circumstances. I say fire and forget because I presume you don't need to hold up the response so you can return what ever you need to the client. And in part-answer to your other question you can return JSON like this

你叫一个fireandforgetfunction(信息);在这里,它可以写日志、发送到SQS或执行其他子派生或进程中。这甚至可能是回调,但我认为,在大多数情况下,失火和遗忘都可能发生。我说fire and forget,因为我认为你不需要拖延反应,这样你就可以把你需要的东西还给客户。作为对另一个问题的部分回答,您可以像这样返回JSON

         db.close();
         var myJSON =[];                            
         sys.puts("Cool info stored and did a non blocking fire and forget for some other mongo monitoring stuff/process and sending control back to the browser");
         sys.puts(sys.inspect(info));//remove later
         myJSON.push({"status":"success"});                     
         myJSON.push({"key1":val1,"key2":val2});//or whatev you want to send
         res.writeHead(200, { "Content-Type" : "text/plain" });
         res.write(JSON.stringify(myJSON));
         res.end();
        }
    }

#1


3  

The question is if all data is added to your database through your node.js app. If so, you can use the EventEmitter class of node.js to trigger an event (http://nodejs.org/api.html#eventemitter-14).

问题是是否所有数据都通过节点添加到数据库中。如果是,可以使用node的EventEmitter类。触发事件(http://nodejs.org/api.html#eventemitter-14)。

If the database is populated by some other app, things are getting difficult. In this case you would need something like a database trigger, which is AFAIK not yet availabled in MongoDB.

如果数据库被其他应用程序填充,事情就会变得越来越困难。在这种情况下,您将需要类似于数据库触发器的东西,这是在MongoDB中还没有使用的AFAIK。

Pushing Events to the Client (aka Comet) will be possible once the HTML 5 websockets API makes its way into all major browsers.

一旦HTML 5 websockets API进入所有主流浏览器,将事件推送到客户端(又名Comet)将成为可能。

In the meantime, you can only try to emulate this behaviour using techniques like (long-term) AJAX polling, forever frame etc. but each of them has its weaknesses.

与此同时,您只能尝试使用诸如(长期的)AJAX轮询、永远框架等技术来模拟这种行为,但每种方法都有其弱点。

#2


3  

I would turn on replication in your mongodb. There is a replicate? database that contains a list of changes, similar to the mysql replication log. You can monitor that.

我将在mongodb中打开复制。有一个复制?包含更改列表的数据库,类似于mysql复制日志。您可以监视。

-daniel

丹尼尔

#3


1  

Almost 3y since the last answer. I would suggest looking at:

从上次回答到现在已经快3年了。我的建议是:

npm install mubsub should get you there

npm安装mubsub会让你到达那里

#4


0  

collection.insert({"key1":val1,"key2":"val2"}, function(err, info){
if(err){ //handle this } else{ if(info){

收集。插入({“key1”:val1,“key2”:“val2”},函数(err, info){if(err){// /处理此}else{if(info)} {

you call a fireandforgetfunction(info); here that can write to logs or send to SQS or do some other child spawn or in process thing. This could even be a callback but I think a fire and forget may do in most circumstances. I say fire and forget because I presume you don't need to hold up the response so you can return what ever you need to the client. And in part-answer to your other question you can return JSON like this

你叫一个fireandforgetfunction(信息);在这里,它可以写日志、发送到SQS或执行其他子派生或进程中。这甚至可能是回调,但我认为,在大多数情况下,失火和遗忘都可能发生。我说fire and forget,因为我认为你不需要拖延反应,这样你就可以把你需要的东西还给客户。作为对另一个问题的部分回答,您可以像这样返回JSON

         db.close();
         var myJSON =[];                            
         sys.puts("Cool info stored and did a non blocking fire and forget for some other mongo monitoring stuff/process and sending control back to the browser");
         sys.puts(sys.inspect(info));//remove later
         myJSON.push({"status":"success"});                     
         myJSON.push({"key1":val1,"key2":val2});//or whatev you want to send
         res.writeHead(200, { "Content-Type" : "text/plain" });
         res.write(JSON.stringify(myJSON));
         res.end();
        }
    }