Diego
Diego

Reputation: 503

Saving timeStamp with coordinates geojson

I'm using GeoJson to save some coordinates with a timeStamp to a Mongoose database, I defined the model like this:

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

});

And the data is sent from the API like this:

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

Where lastLocation is:

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

When I make a post to the route the coordinates are saved correctly but the timeStamp doesn't save in the document.

Upvotes: 2

Views: 1428

Answers (1)

jtmarmon
jtmarmon

Reputation: 6169

In this code:

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

You are pushing a date for properties, when the date value is actually located at properties.timeStamp

So you can use this instead

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

Upvotes: 2

Related Questions