mongodb – Mongoose JS承诺?或者如何管理批量保存

栏目: JavaScript · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/11913074/mongoose-js-promises-or-how-to-manage-batch-save
如何在Mongoose中管理批量保存?我看到它可能还不可能:

> How can I save multiple documents concurrently in Mongoose/Node.js?

有人提到使用像 q 这样的流量控制库,但是我也注意到 promises 在mongoose中,它可以用吗?我可以在jQuery Deferred / Promises中做

$.when(obj1.save(), obj2.save(), obj3.save()).then ->
    # do something?
尝试 async 模块的 parallel 功能.
var functions = [];

for (var i=0; i < docs.length; i++) {
    functions.push((function(doc) {
        return function(callback) {
            doc.save(callback);
        };
    })(docs[i]));
}

async.parallel(functions, function(err, results) {
    console.log(err);
    console.log(results);
});

翻译自:https://stackoverflow.com/questions/11913074/mongoose-js-promises-or-how-to-manage-batch-save


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Nature of Code

The Nature of Code

Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95

How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具