Skip to content
Advertisement

Mongodb: how to create a `tail -f` view on a capped collection?

I would like to create a sort of a ‘dashboard’ on a capped collection (which is used as a log table) on my Mongo database. This is how I create the collection:

JavaScript

I do a collection.find(), with options tailable:true, awaitdata:true, and numberOfRetries:-1 (infinite retries).

What puzzles me is that I’d expect the find().each() loop to wait for new data (messages)… instead (after a few seconds) it errors out (with No more documents in tailed cursor… :-()

This is the code I’m working with:

JavaScript

What do I miss?

UPDATE:

Not having received any conclusive answer until now, I deduce MongoDb tailable collections is not ready for prime time… :-(((

Sadly giving up for a more classic and robust fs logging solution…

Advertisement

Answer

You could set up subscriber function that subscribes for new MongoDB documents using the tailable find() cursor as a node.js stream. The following demonstrates this:

JavaScript

Source: How to subscribe for new MongoDB documents in Node.js using tailable cursor

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement