The script I am working with is here: https://gist.github.com/2759751
I want to modify some code to store a streaming, parsed message from a node.js app to a mongodb collection for later access. So far I am working with tutorial code and am confused about getting the db.open() mechanism just right, if I use db.open(), and since it's a stream, it complains that it can't open it multiple times. If I remove it, it says it can't find an open connection. How do I open the connection one time to allow storing of the stream?
I instantiate mongo with:
mongo = require('mongodb'),
db = new mongo.Db('wikis', new mongo.Server('localhost', 27017, {}), {});
the stream kicks out an object like this:
return {
flag: flag,
page: page,
pageUrl: pageUrl,
url: m[3],
delta: delta,
comment: m[6],
wikipedia: wikipedia,
wikipediaUrl: wikipediaUrl,
wikipediaShort: config.wikipedias[msg[0]].short,
wikipediaLong: config.wikipedias[msg[0]].long,
user: user,
userUrl: userUrl,
unpatrolled: isUnpatrolled,
anonymous: anonymous,
robot: isRobot,
namespace: namespace,
minor: isMinor
}
And I want to store parts of the stream like this:
function saveRecs(msg){
//db.open(function(){
db.collection('wikiCollection', function(err, collection){
doc = {
"page": msg.page,
"url": msg.url,
"user": msg.user
};
collection.insert(doc, function(){
console.log('Got a record, boss!');
});
});
//});
}
The db.open takes a callback like this
dbOpenCallback(err, db){
// Check if err is set to anything other wise you are good
}
//...
db.open(dbOpenCallback);
Once you have established the connection is open and ok to go, your saveRecs function will be ok to call as long as the db variable is in scope.