Search code examples
mongodbtriggerscommandqueuetimed

Mongo as an event Triggerer


I have this feel that mongoDB could also be used a event trigger system by using mongo's server-side javascript capability.

for example

    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");})})

Output =========

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

Not explored all possibilities of this, but just this thought brings in the following 1) Trigger event system with the particular collection acting as a delayed command queue. 2) Delayed/timed persistance

Any more uses of this?


Solution

  • No, MongoDB does not have triggers: https://jira.mongodb.org/browse/SERVER-124

    The way you trigger something there is not a healthy way to do it, not even sure what that will output to the collection itself, probably the object of the function or a null value.

    Also I believe this will only work in the console and never in a client side program unless you use eval (maybe, not even sure then), and, well, I don't even need to explain why that is bad.

    You could just use active record (or something similar) instead to accomplish this.