Mongoose:使用promises插入一个依赖于另一个的集合

时间:2016-04-29 19:23:17

标签: mongodb mongoose promise

我需要在两个集合中插入记录。第二个集合存储第一个集合的记录ID。这是1:m(fisrt:second)的情况。触发器是第二个集合:

  1. 如果需要存储第二个集合的记录
  2. 检查第一个集合中是否已有适合的记录
  3. 如果不是:然后在第一个集合中保存一个
  4. 存储第二个集合
  5. 保存第二个集合中第一个集合的记录的ID
  6. 以下示例似乎已满足这些步骤。但我只有一半的承诺。 如何以更好的“承诺”方式完成这项工作?

    saveObjects(name: String, objects: Array<IObject>){
    
            var promise = FirstModel.findOne({Name : name}).exec();
            promise.then(function(res1){
                if (!res1){
                    var la = new FirstModel();
                    la.Name = name;
                    la.save(function(err){
                        if (err) throw err;
                    })
                }
            }).error(function(err){
                throw err;
            })
    
            objects.forEach(function(obj) {
                FirstModel.findOne({Name : name},'_id',function(err, res2){
                    if (err) throw err;
    
                    var vo = new SecondModel();
                    vo.Name = name;
                    vo.FistID = res2._id;
    
                    vo.save(function(err){
                        if (err) throw err;
                    });
                });
            });
    
    }
    

1 个答案:

答案 0 :(得分:0)

我在这里假设您正在使用蓝鸟或其他承诺等同设置。

var Promise = require('bluebird');
saveObjects(name: String, objects: Array < IObject > ) {

    // Get the first model
    return FirstModel
        .findOne({
            Name: name
        })
        .exec()
        .then(function (res1) {
            if (!res1) {
                var la = new FirstModel();
                la.Name = name;
                return la.save() // We return the save operation on the model, save() returns a promise
            }

            // We just return the model normally so it passes down the chain.
            return res1;
        })
        .then(function (res1) {
            // Here we use Promise.all() method on bluebird which accepts an Array
            // of promises, we create a promise from the objects array by using Array.map() which
            // goes through every object in the array and creates a new array. 
            return Promise
                .all(objects.map(function (obj) {
                    // We go through each object in objects and create a new 
                    // model and return the save() method of each model, this
                    // creates a array of promises which are resolved when each
                    // all model has been saved.
                    var vo = new SecondModel();
                    vo.Name = name;
                    vo.FistID = res1._id;

                    return vo.save();
                }));
        })
        .then(function (models) {
            // here we have all the models we just saved in an array called models,
            // do what you want with it.
        })
        .error(function (err) {
            throw err;
        })
}

有关详情,请参阅Array.map() herePromise.all() here

上的文档