gpt4 book ai didi

node.js - Mongoose Bulk 不写入默认值

转载 作者:太空宇宙 更新时间:2023-11-03 21:49:26 26 4
gpt4 key购买 nike

我有以下架构:

//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) {...});

关于node.js - Mongoose Bulk 不写入默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41199635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com