I'm fairly new to Mongoose and MongoDB itself and I'm trying to save a bunch of documents inserted via insertMany method but it is not saving the docs.
Here is my code:
Model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var hostSchema = new Schema({
hostname: String,
timestamp: Number,
});
var hostModel = mongoose.model('host', hostSchema, 'host');
module.exports = hostModel;
ExpressJS Post route
var mongoose = require('mongoose');
var hostModel = require('../../models/Host');
router.post('/host', function (req, res, next) {
var payload = req.body;
(async function(){
var host = new hostModel();
const insertMany = await hostModel.insertMany(payload.data);
console.log(JSON.stringify(insertMany,'','\t'));
const saveMany = await hostModel.save();
res.status(200).send('Ok');
})();
});
That console.log
shows me the records but when I do hostModel.save()
I get hostModel.save is not a function
.
How can I save the inserted docs?
Thanks very much for the help!