Skip to content

Instantly share code, notes, and snippets.

@andreareginato
Created October 31, 2012 11:32
Show Gist options
  • Save andreareginato/3986569 to your computer and use it in GitHub Desktop.
Save andreareginato/3986569 to your computer and use it in GitHub Desktop.
mongodb native driver for node.js patch to work with capped collections
// -------------------------------------------------
// MongoDB extension to get back the new records
// added to a capped collection. This code will not
// be used if mongoose is found out to be reliable
//
// var events = require('./lib/events');
//
// events.connect(function(collection) {
// events.execute(collection);
// });
// --------------------------------------------------
var mongodb = require('mongodb')
, QueryCommand = mongodb.QueryCommand
, Cursor = mongodb.Cursor
, Collection = mongodb.Collection;
// ----------------------------------------------------------------------------
// Read data from the capped collection.
// (known bug: if there are no documents in the collection, it doesn't work.)
// ----------------------------------------------------------------------------
exports.execute = function(collection) {
collection.find({}, { 'tailable': 1, 'sort': [[ '$natural', 1 ]] }, function(err, cursor) {
cursor.each(function(err, item) {
if(item != null) {
console.log("Taking care of the event identified by the ID", item._id);
}
});
});
};
// ---------------
// DB connection
// ---------------
exports.connect = function(callback) {
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('jobs_test', server, { safe:true }).open(function (error, db) {
db.collection("events", function (err, collection) {
collection.isCapped(function (err, capped) {
if (err) { console.log ("Error when detecting capped collection. Aborting. Capped collections are necessary for tailed cursors."); process.exit(1); }
if (!capped) { console.log (collection + " is not a capped collection. Aborting. Please use a capped collection for tailable cursors."); process.exit(2); }
console.log ("Successfully connected to the collection jobs_test:events.");
callback(collection);
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment