【发布时间】:2020-03-11 23:23:33
【问题描述】:
我对 Mongoose 和 MongoDB 本身还很陌生,我正在尝试保存通过 insertMany 方法插入的一堆文档,但它没有保存文档。
这是我的代码:
型号:
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 发布路径
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');
})();
});
console.log 向我显示记录,但当我这样做时 hostModel.save() 我得到hostModel.save is not a function。
如何保存插入的文档?
非常感谢您的帮助!
【问题讨论】:
-
请使用该链接您可以找到详细信息stackoverflow.com/questions/37697448/…