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

相關文章
相關標籤/搜索