moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

How I can reformat the mongodb message format to store json message data? #763

Open supreethkm opened 5 years ago

supreethkm commented 5 years ago

HI,

How I can reformat the mongodb message format to store json message data? The message should be stored as JSON object along with the custom fields which will be added in the server/broker side.

Thanks.

ravenOSS commented 5 years ago

I think you mean 'How to store MQTT message as JSON into mongoDB'. If so, you could configure Mosca with mongodb and the persisted data would, by default, be stored as JSON. I cannot find the reference, but I believe that the persisted message store is a capped collection. So if you are expecting to store a lot of data, it would require a large cap to be set so that you do not lose data. Another solution would be to create a subscriber and use a mongodb driver to write to a standard collection in a mongodb database. You could use this project as a starting point: https://github.com/dennisdegreef/mqtt-mongo-recorder Just be careful with the driver and database versions you are using. The latest versions of mongodb returns a client to which you 'attach' a database. You do not specify the database in the uri unless it is the one you wish to authenticate against. David

ravenOSS commented 5 years ago

Also, I think that the persisted data store has a TTL (time to live) parameter that you'd have to take a look at. David