Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Mongo作为事件触发器_Mongodb_Triggers_Command_Queue_Timed - Fatal编程技术网

Mongodb Mongo作为事件触发器

Mongodb Mongo作为事件触发器,mongodb,triggers,command,queue,timed,Mongodb,Triggers,Command,Queue,Timed,我觉得通过使用mongo的服务器端javascript功能,mongoDB也可以用作事件触发系统 比如说 db.system.js.save( { _id : "timer", value : function timer(interval,times,fun){ var s=new Date(); var e=new Date(); var cnt=0; while(cnt<times){ print("audit: "+cnt); eva

我觉得通过使用mongo的服务器端javascript功能,mongoDB也可以用作事件触发系统

比如说

    db.system.js.save(
    {
        _id : "timer",
        value : function timer(interval,times,fun){ var s=new Date(); var e=new Date(); var cnt=0; while(cnt<times){ print("audit: "+cnt); eval(fun()); cnt++; s=new Date(); while(e-s < interval){e=new Date();}}};
    }
db.testcol.save({a:timer(1000,3,function(){print(Date() + "Message");})})
db.system.js.save(
{
_id:“计时器”,

value:function timer(interval,times,fun){var s=new Date();var e=new Date();var cnt=0;而(cntNo,MongoDB没有触发器:

您触发某些内容的方式不是一种健康的方式,甚至不确定将输出到集合本身的内容,可能是函数的对象或空值

此外,我相信这只会在控制台中起作用,在客户端程序中永远不会起作用,除非您使用
eval
(也许,甚至不确定),而且,我甚至不需要解释为什么这是不好的


您可以使用active record(或类似工具)来完成此操作。

绝对不推荐。。这会在eval运行时阻止对数据库的所有其他读写操作。这不是事件触发器,而是数据库服务器bazooka;-)。什么是活动记录?@Sreesankar active record是一个范例,通过实现一个模型来表示程序中当前查看的记录集。嗯,ruby on rails就是一个很好的例子,它默认使用ORM活动记录。 audit: 0 Sun Mar 03 2013 16:29:46 GMT+0530 (IST)Message audit: 1 Sun Mar 03 2013 16:29:47 GMT+0530 (IST)Message audit: 2 Sun Mar 03 2013 16:29:48 GMT+0530 (IST)Message