Mongoose 批量插入文档
var TestSchema = new mongoose.Schema({
/ Test Schema /
})
var TestModel = mongoose.model('Test', TestSchema);
model.create()
TestModel.create({ candy: 'jelly bean' }, { candy: 'snickers' }, function (err, jellybean, snickers) {
});
Model.collection.insert( )
Model.collection.insert(docs, options, callback)
-
docs - 要插入的文档是数组;
-
options - 可选的配置对象- 请参见 docs
-
callback(err, docs) - 保存完所有文档或出现错误后调用,获取之后, 将调用否则出错。 如果成功, docs 是文档的保存的数组。
此方法会跳过任何验证过程和直接访问Mongo驱动程序,因此适合大批量插入数据。
// 文档组
var docs = [/ a humongous amount of potato objects /];
TestModel.collection.insert(docs, onInsert);
function onInsert(err, docs) {
if (err) {
// TODO: handle error
} else {
console.info('%d potatoes were successfully stored.', docs.length);
}
}
参见官方文档相关函数。
关键字:mongodb, docs, 文档, err