3

I'm trying to develop a Log Viewer using DerbyJS, Racer and MongoDB. The logs will be inserted into the MongoDB database by a different source continuously, and my Log Viewer should be able to update the Logs Table on the user interface automatically.

I was wondering if there is a native way of listening to MongoDB events, like:

 - On update
 - On delete

These would be similar to, for example, Oracle DB triggers.

F. Santiago
  • 842
  • 10
  • 18

3 Answers3

10

You can listen to events like insert, update, and other data events in mongodb using special collection named oplog. You just need to enable replication on your db instance either using mongod --master or mongod --replicaSet.

Oplog is actually a capped collection which is used by mongodb internally to implement replication. If you are using master/slave replication you will find the collection by the name of oplog.$main, if you are using replica sets it will be named oplog.rs.

You can use a tailable cursor on oplog, that should work.

Oplog, in effect, is logs itself. So you might not need to store them separately for logging purpose. However its size is fixed. Meaning when its full, older data gets deleted.

Also make sure you are looking into the local database, thats where oplogs are maintained

Here is a working example from mongoskin wiki page

skin = require "mongoskin"
db = skin.db "localhost:27017/local"

#//Cursor on oplog (a capped collection) which maintains a history for replication
#//oplog can be used only when replication is enabled
#//Use oplog.rs instead of oplog.$main if you are using replica set

oplog = db.collection "oplog.$main"
cursor = oplog.find({'ns': "icanvc.projects"},{tailable: yes, awaitData: yes})

#//Using cursor.nextObject will be slow
cursor.each (err, log)->
    console.error err if err
    console.log log if not err
Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
4

The typical approach to a log viewer application is to use a tailable cursor with a capped collection of log entries.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • Thank you, I will try it and see if it serves the purpose :) I'll give feedback meanwhile. Do you think I need the capped collection to make this work? To give that trigger effect, or could I just use the tailable cursor? – F. Santiago Aug 06 '12 at 08:41
  • Yes, tailable cursors can only be used with capped collections. – JohnnyHK Aug 06 '12 at 14:09
1

No, https://jira.mongodb.org/browse/SERVER-124 it has to be application side.

I am unsure as to whether node.js has inbuilt triggers for MongoDB within it's driver however most likely not so you will need to code this in yourself.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • I it doesn't. I used the definition of trigger the wrong way. I mean't what you said, updating the application on database changes, but it has to be the app to query the database. Can't really be the other way around. – F. Santiago Aug 06 '12 at 14:02
  • Day without Unresolved [issue|feature|buq|problem] in Mongodb is not the same day :) Date of 1st Reply: 14/Jul/09 – user956584 Sep 13 '12 at 19:22