Project array content with mongodb Project array content with mongodb mongoose mongoose

Project array content with mongodb


You need to do an $unwind operator on the Location array first in your aggregation pipeline, then $group the resulting documents to apply the accumulator expressions $first and $last.

Your aggregation pipeline should look like this (untested):

db.foo.aggregate([    { "$unwind": "$location" },    {        "$group": {            "_id": "$_id",            "longitude": { "$first": "$location" },            "latitude": { "$last": "$location" }        }    }]);