How create a new array field with the aggregate framework

前端 未结 4 2031
礼貌的吻别
礼貌的吻别 2021-02-15 15:51

I\'m starting to use mongoDb and I\'m stuck with a simple use case.

Let\'s say I\'ve got a collection \'aCollection\' with entries such as this:



        
4条回答
  •  生来不讨喜
    2021-02-15 16:28

    It looks like MongoDB 3.2 provides fairly simple and elegant way to create GeoJSON points using aggregation framework.

    We had to perform the transformation on approximately 2 Million records two times a day, so aggregation framework was the fastest and probably most reliable approach.

    Below is a Mongoose example of how to transform geolocation data from collection with longitude/latitude into a collection with GeoJSON points.

    Locations
        .aggregate([
            {
                $project : {
                    _id: 0,
                    "location": {
                        "type": { $literal: "Point" }, 
                        "coordinates": ["$longitude", "$latitude"]
                    }
                }
            },
            {
                $out : 'test_1'
            }])
        .exec(function(err,data) {
            if (err) {
                console.error(err);
            } else {
                console.log("Done transforming.");
            }
        });
    

提交回复
热议问题