Mongoose 批量插入文档

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 - 要插入的文档是数组;html

  • options - 可选的配置对象- 请参见 docsnode

  • callback(err, docs) - 保存完全部文档或出现错误后调用,获取以后, 将调用不然出错。 若是成功, docs 是文档的保存的数组。api

此方法会跳过任何验证过程和直接访问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);
    }
}

参见官方文档相关函数。mongoose

相关文章
相关标签/搜索