使用坐标geojson保存timeStamp

时间:2015-03-11 14:19:37

标签: node.js mongodb mongoose

我使用GeoJson将一些带有timeStamp的坐标保存到Mongoose数据库,我定义了这样的模型:

var positionSchema = mongoose.Schema({
position: [{
    properties: {
        timeStamp: Date
    },
    geometry: {
        type: {
            type: String,
            default: 'Point'
        },
        coordinates: {
            type: [Number]
        }
    }
}]

});

数据从API发送如下:

 position.findByIdAndUpdate(values.geometries, {
                  $push: {
                    position: {
                      properties: request.payload.timeStamp,
                      geometry: lastLocation
                    }
                  }
                }

lastLocation是:

lastLocation = {
                    type: 'Point',
                    coordinates: [
                      request.payload.position.longitude,
                      request.payload.position.latitude
                    ]
                  };

当我在路线上发帖时,坐标会正确保存,但timeStamp不会保存在文档中。

1 个答案:

答案 0 :(得分:2)

在此代码中:

position.findByIdAndUpdate(values.geometries, {
                  $push: {
                    position: {
                      properties: request.payload.timeStamp,
                      geometry: lastLocation
                    }
                  }
                }

当日期值实际位于properties

时,您正在推送properties.timeStamp的日期

所以你可以改用

position.findByIdAndUpdate(values.geometries, {
                  $push: {
                    position: {
                      'properties.timeStamp': request.payload.timeStamp,
                      geometry: lastLocation
                    }
                  }
                }