How to create a new collection on every POST request on NodeJS express + mongoose based REST API ?
There are lot of tutorials that uses the same collection hard-coded, but can't find one which allows to create new collection on every new POST.
For example:
model.js
var mongoose = require('mongoose');
var mySchema = new mongoose.Schema({
//Scehma Here
});
module.exports = mongoose.model('mycollection', mySchema);
and server.js
var devices = require('./model');
router.route('/devices')
.post(function(req, res) {
var device = new devices();
device._id = req.body.id;
device.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Success!' });
});
})
This creates new document on the mycollection
for every new POST
But, I need to isolate every new object created in to a new collection.
Is it possible to create a new collection on every POST with the same schema and collection name being req.body.id
Update:
Usecase for @chridam and @MykolaBorysyuk comments
The data will be timeseries data from lot of IoT devices.
On the first connect, the device will do a POST
request with its ID
like IMEI and from then it will send data continuously every 5 secs.
To store something like here: http://blog.mongodb.org/post/65517193370/schema-design-for-time-series-data-in-mongodb
I thought creating a new collection for each device will be good idea, if thats a bad choice (I'm DevOps and new to Development), please suggest me a better approach for the above use case. It will be 100s of devices with continuous time series data.
Thank you.