mongoose批量不写入默认值

我有以下的模式:

function gtElInit() {var lib = new google.translate.TranslateService();lib.translatePage('en', 'zh-CN', function () {});}//commands.js const mongoose = require('bluebird').promisifyAll(require('mongoose')); // define the schema for our account data const commandsSchema = mongoose.Schema({ cmdName : { type: String, required: true, unique: true}, description: {type: String}, help: {type: String}, accessRequired: {type: Number,default: 0}, enabled: {type: Boolean, default: true } },{timestamps : true}); module.exports = mongoose.model('Commands', commandsSchema); 

如果我添加一个这样的新命令:

 let addCommand = new Command(); addCommand.cmdName= 'whois'; addCommand.description = 'Retrieve character information from server.'; addCommand.help = '!whois <character name>'; addCommand.save(); 

一切工作正常,默认值编写但是如果我尝试插入多个命令的默认值不添加到数据库,这里是我使用的代码:

 let cmdList = []; cmdList.push({ cmdName: 'whois', description: 'Retrieve character information from server.', help: '!whois <character name>', }); cmdList.push({ cmdName: 'shutdown', description: 'Shutdown bot.', help: '!shutdown' }); Command.collection.insert(cmdList, { w: 0, keepGoing: true }, function(err) { if (err) { console.log(err); } }); 

你通过调用Command.collection.insert来有效地绕过Mongoose,所以这就是为什么你没有得到默认值。

相反,使用Model.insertMany来执行批量插入Mongoose的方式:

 Command.insertMany(cmdList, function(err) {...});