小编Unm*_*eow的帖子

具有异步队列和瀑布的猫鼬

我的目标是通过 Mongoose 导入大量数据。作为新手,我无法通过异步使用各种机制正确设置流量控制。很高兴有人可以指出适当的解决方案。谢谢。

var async = require('async'),
    mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');
var Cat = mongoose.model('Cat', { name: String });

// Imagine this is a huge array with a million items.
var content = ['aaa', 'bbb', 'ccc'];
var queries = [];
content.forEach(function(name) {
  queries.push(function(cb) {
    var obj = new Cat({ name: name });
    obj.save(function(err) {
      console.log("SAVED: " + name);
      console.log(err);
    });
    return true;
  });
});

// FAILED: async.parallel adds all content to db, 
// but it would exhaust the resource with …
Run Code Online (Sandbox Code Playgroud)

asynchronous mongoose mongodb node.js

2
推荐指数
1
解决办法
4283
查看次数

标签 统计

asynchronous ×1

mongodb ×1

mongoose ×1

node.js ×1