Arrays 使用mongodb创建项目数组内容

Arrays 使用mongodb创建项目数组内容,arrays,mongodb,mongoose,coordinates,geospatial,Arrays,Mongodb,Mongoose,Coordinates,Geospatial,我的问题是,当我对坐标使用MongoDB 2D索引时,一切正常,但JSON输出不是我想要的 var schema=new Schema({ location:{ type:[Number], // [<longitude>, <latitude>] index:'2d', required:false } }); **desired outp

我的问题是,当我对坐标使用MongoDB 2D索引时,一切正常,但JSON输出不是我想要的

    var schema=new Schema({
        location:{
            type:[Number],  // [<longitude>, <latitude>]
            index:'2d',
            required:false
        }
    });

    **desired output**

    {
        "_id":"xxx",
        "longitude":18.056974411010742,
        "latitude":54.31120601701069,
    }

    **what i get**

    {
        "_id":"xxx",
        "location":[18.056974411010742,54.31120601701069]
    }


    **What i tried to do**
    db.foo.aggregate(
        {$project:{
        _id:1,
        longitude:"$location.0",
        latitude:"$location.1"
    }});
var模式=新模式({
地点:{
类型:[编号],//[,]
索引:'2d',
必填项:false
}
});
**期望输出**
{
“_id”:“xxx”,
“经度”:18.056974411010742,
“纬度”:54.31120601701069,
}
**我得到了什么**
{
“_id”:“xxx”,
“位置”:[18.056974411010742,54.31120601701069]
}
**我想做什么**
db.foo.aggregate(
{$项目:{
_id:1,
经度:“$location.0”,
纬度:“$location.1”
}});
但很明显,这是行不通的。我希望我对这个问题解释得足够好。谢谢。

您需要在聚合管道中首先对位置数组执行运算符,然后对生成的文档应用累加器表达式和

您的聚合管道应如下所示(未经测试):


谢谢,这就成功了!
db.foo.aggregate([
    { "$unwind": "$location" },
    {
        "$group": {
            "_id": "$_id",
            "longitude": { "$first": "$location" },
            "latitude": { "$last": "$location" }
        }
    }
]);