Created
February 6, 2014 16:28
-
-
Save codepope/8847630 to your computer and use it in GitHub Desktop.
A simple MQTT to MongoDB bridge for Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mqtt=require('mqtt'); | |
var mongodb=require('mongodb'); | |
var mongodbClient=mongodb.MongoClient; | |
var mongodbURI='mongodb://username:password@server.mongohq.com:port/database'; | |
var deviceRoot="demo/device/"; | |
var collection,client; | |
mongodbClient.connect(mongodbURI,setupCollection); | |
function setupCollection(err,db) { | |
if(err) throw err; | |
collection=db.collection("test_mqtt"); | |
client=mqtt.createClient(1883,'localhost'); | |
client.subscribe(deviceRoot+"+"); | |
client.on('message', insertEvent); | |
} | |
function insertEvent(topic,message) { | |
var key=topic.replace(deviceRoot,''); | |
collection.update( | |
{ _id:key }, | |
{ $push: { events: { event: { value:message, when:new Date() } } } }, | |
{ upsert:true }, | |
function(err,docs) { | |
if(err) { | |
console.log("Insert fail"); // Improve error handling | |
} | |
} | |
); | |
} | |
Yes, this is quite old code and the Mongo libraries changed somewhat....
Roughly where it hands over a db in setupCollextion, now it hands over a MongoDB client. You can then get the db with client.db("dbname")....
The rest should work fine though I'd write it completely differently now.
Yeah I figured it out. Thanks for the information.
…On Fri 28 Feb, 2020, 04:21 Dj Walker-Morgan, ***@***.***> wrote:
Yes, this is quite old code and the Mongo libraries changed somewhat....
Roughly where it hands over a db in setupCollextion, now it hands over a
MongoDB client. You can then get the db with client.db("dbname")....
The rest should work fine though I'd write it completely differently now.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/8847630?email_source=notifications&email_token=ANDY6XBPWZ65PY66ZVS55PLRFA7WJA5CNFSM4K4ZKH4KYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGC3AY#gistcomment-3192332>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANDY6XGOTYB3NJIJBSSU6A3RFA7WJANCNFSM4K4ZKH4A>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After further investigation and looking at the age of this gist, looks like a version mismatch. https://stackoverflow.com/questions/47662220/db-collection-is-not-a-function-when-using-mongoclient-v3-0
It gets past this point if you used mongodb@2 on the npm install. It then errors out on createClient as not being a function, so its likely that the mqtt library has changed some too. @Shivsk007 check your versions.
Dropping mongodb to 2 and mqtt to 1 got me running with "createClient is deprecated, use connect instead" displayed.
Getting mine working with current versions, tho this gist should probably be a full on project to include a package.json or have comments with the versions to install at the top.
Not being whiny, this code is 6 years old and clearly still useful :D Thanks for the work!