【问题标题】:MongoSkin wrong insertionMongoSkin错误插入
【发布时间】:2015-03-16 11:40:28
【问题描述】:

我有一个包含以下结构的国家/地区的数组:

{
    "code": "ZW",
    "name": "Zimbabwe",
    "zipPattern": "[\\s\\S]*",
    "states": [
        {
            "name": "Bulawayo"
        },
        {
            "name": "Harare"
        },
        {
            "name": "Manicaland"
        },
        {
            "name": "Mashonaland Central"
        },
        {
            "name": "Mashonaland East"
        },
        {
            "name": "Mashonaland West"
        },
        {
            "name": "Masvingo"
        },
        {
            "name": "Matabeleland North"
        },
        {
            "name": "Matabeleland South"
        },
        {
            "name": "Midlands"
        }
    ]
}

我正在尝试使用带有以下代码的MongoSkin 将它们插入MongoDb

var countries = require('./mongo/ready/Countries');
db.collection('countries').find().toArray(function (err, result) {
  if (result.length === 0) {
    for (var i = 0; i < countries.length; i++) {
        var obj = countries[i];
        var states = obj.states;
        db.collection('countries').insert({
            name: obj.name,
            code: obj.code,
            zipPattern: obj.zipPattern
        }, function (error, countryResult) {
            var id = countryResult[0]._id;

            for (var j = 0; j < states.length; j++) {
                var state = states[j];
                db.collection('states').insert({
                    countryId: id,
                    name: state.name
                }, function (stateError, stateResult) {
                    if (stateError) console.log(stateError);
                     console.log(stateResult);
                });
            }
        });
    }
  }
});

但代码会为数组中的每个国家/地区插入数组中最后一个国家(津巴布韦)的州,而不是正确的州。我该如何解决?

【问题讨论】:

    标签: node.js mongodb mongoskin


    【解决方案1】:

    一般我们不会在同步循环(简单的for循环)之间使用异步查询(插入)。它给我们异常的结果。 Node 提供了异步循环来克服这个问题。

    首先需要异步模块。

    var async = require('async');
    

    现在您可以使用以下代码插入国家/地区及其各自的州

    async.each(countries, function(obj, callback) {
    
        var states = obj.states;
        db.collection('countries').insert({
            name: obj.name,
            code: obj.code,
            zipPattern: obj.zipPattern
        }, function(error, countryResult) {
            if (error) {
                callback(error);
            } else {
                var id = countryResult[0]._id;
                async.each(states, function(state, callback) {
                    db.collection('states').insert({
                        countryId: id,
                        name: state.name
                    }, function(stateError, stateResult) {
                        if (stateError) {
                            callback(stateError);
                        } else {
                            callback();
                        }
    
                    });
                });
                callback();
            }
        }); }, function(err) {
        if (err) {
            // handle error here
        } else {
           // do stuff on completion of insertion
        } });
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 1970-01-01
      • 2014-07-26
      • 2013-12-13
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多