mongoose-querystream-worker更新文档

时间:2015-02-15 16:43:36

标签: node.js mongodb mongoose

我收集了很多人,我会在每个人的网站上引用。这个集合在mongodb上,因此我使用nodejs作为服务器来循环每个文档,然后使用有关地理位置的信息更新一个。 我写这段代码

......
    app.get('/', function (req, res) {
      People.find().stream().concurrency(2).work(
        function (doc, done) {
            geocoder.geocode({address: doc.address, country: 'Italy', zipcode: doc.cap}, function(error, value) {
              if(!error){
                doc.loc.coordinates[0]=value[0].latitude;
                doc.loc.coordinates[1]=value[0].longitude;
                doc.save( function(error, data){
                  if(error){
                    console.log(error);
                  }
                });
                console.log(doc.surname+" "+doc.name+" "+doc.city);
                done();
              }else{
                console.error("!!!!!!!!!"+error+"!!!!!!!!!!!!!\n"+doc.surname+" "+doc.name+" "+doc.city);
              }
            });
        }, 
        function (err) {
          console.log(doc.surname+" "+doc.name+" "+doc.city+" completed!");
        }
      );
........

这是架构

var mongoose = require("mongoose");
var GeoJSON = require('mongoose-geojson-schema');

//connect to database
var db = mongoose.connect('mongodb://127.0.0.1:27017/db',function (error) {
    if (error) {
        console.log(error);
    }
});

mongoose.connection.on('connected', function () {
  console.log('Mongoose default connection open');
});

var peopleSchema = new mongoose.Schema({
  surname:  String,
  name: String,
  city_birthday: String,
  birtday:   { type: Date},
  address: String,
  city: String,
  cap: Number,
  sex: String,
  type: String,
  loc:GeoJSON.Point
},{ collection: 'people' });

module.exports = db.model('people', peopleSchema);

此代码有效但我查询收藏时无法查看更新。 我不明白问题出在哪里

0 个答案:

没有答案