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 | |
} | |
} | |
); | |
} | |
sh1vkumar
commented
Feb 29, 2020
via email
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