Reshape of lat and lon coordinates in MongoDB, using the aggregate pipeline
Reshape of lat and lon coordinates in MongoDB, using the aggregate pipeline Summary To transform a large number of documents in a MongoDB collection with spatial data, for example: {lat: -58.1, lon: -34.2} to a GeoJson format, recognizable by MongoDB spatial analysis functions, for example: {type: ”Point”, location: [- 58.1, -34.2]} It seems advisable to use the aggregation framework: db.tweets.aggregate ([{$project: {location: {type: "Point", coordinates: ["$lon", "$lat"]}}}, $ out: {$out: "newcollectionname"}] ); The problem An usual task in the database in MongoDB may be to prepare the data for spatial tasks. As I described in the previous post, it is necessary to have the data in the compatible format, in this case as a GeoJson. For example, if we are working with points: {type: ”Point”, location: [- 58.1, -34.2]} {type: ”Point”, location: [- 58.1, -34.2]} That is, specifying a "type" key that specifies that it is a point, and then a "location" key with an array of the coordinates pair: longitude and latitude (in that order!) If working with polygons: {type: "Polygon", coordinates: [[[0, 0], [3, 6], [6, 1], [0, 0]]]} The problem with my data is that it was in the following format and I needed to do…