2016-01-09 25 views
0

我有两个销售数据集合 - 一个用于城市,一个用于城市内的城镇。我只有城镇级别的销售数据(按月),现在想要添加一个新的city.total销售字段,它将成为每个城市内城镇销售额的总和。mongodb/node - 从游标更新单独的集合 - 承诺条件

我正在使用节点来运行脚本。

  1. 在城镇集合(聚合)上设置一个光标,以将城市级别的所有销售分组。这工作正常。
  2. 迭代游标,为每个城镇找到城市并将该值添加到city.total_sales。

示例代码:

cursor.each(function(err, doc) { 
    assert.equal(err, null); 
    if (doc != null) { 
     // debug - lets just find an example row to update 
     var city_row = db.collection('city').findOne({ "city": "Liverpool" }  
) 
     console.log(city_row); 
    } else { 
     callback(); 
    } 
}); 

我看到的问题 - 控制台显示“无极{}”

这是运行一个批处理过程 - 而不是过分关注目前业绩 - 我需要做些什么才能让代码等待查找,而不是异步操作?

回答

0

将所有找到的一个查询放入Promise数组中,然后使用Promise.all()