如何在Mongodb中查询文档内部

时间:2018-04-03 14:14:25

标签: mongodb mongodb-query aggregation-framework geojson

我正在寻找一个mongodb查询来查找文档中两点之间的距离。我的收藏品包括从TLC获得的属于纽约市的出租车旅行。这个集合包括这样的旅行:

/* 1 */
{
    "_id" : ObjectId("5aa8d5b22d514514d4f95c08"),
    "type" : "Feature",
    "geometry_pk" : {
        "type" : "Point",
        "coordinates" : [
            -73.993896484375,
            40.7501106262207
        ]
    },
    "properties" : {
        "ID_Postgres" : 1,
        "VendorID" : 2,
        "passenger_count" : 1,
        "store_and_fwd_flag" : "N",
        "RatecodeID" : 1,
        "trip_distance" : 1.59,
        "payment_type" : 1,
        "fare_amount" : 12.0,
        "extra" : 1.0,
        "mta_tax" : 0.5,
        "tip_amount" : 3.25,
        "tolls_amount" : 0.0,
        "improvement_surcharge" : 0.3,
        "total_amount" : 17.05,
        "tpep_pickup_datetime" : ISODate("2015-01-15T19:05:39.000Z"),
        "tpep_dropoff_datetime" : ISODate("2015-01-15T19:23:42.000Z")
    },
    "geometry_do" : {
        "type" : "Point",
        "coordinates" : [
            -73.9747848510742,
            40.750617980957
        ]
    }
}

/* 2 */
{
    "_id" : ObjectId("5aa8d5b22d514514d4f95c09"),
    "type" : "Feature",
    "geometry_pk" : {
        "type" : "Point",
        "coordinates" : [
            -74.0016479492188,
            40.7242431640625
        ]
    },
    "properties" : {
        "ID_Postgres" : 2,
        "VendorID" : 1,
        "passenger_count" : 1,
        "store_and_fwd_flag" : "N",
        "RatecodeID" : 1,
        "trip_distance" : 3.3,
        "payment_type" : 1,
        "fare_amount" : 14.5,
        "extra" : 0.5,
        "mta_tax" : 0.5,
        "tip_amount" : 2.0,
        "tolls_amount" : 0.0,
        "improvement_surcharge" : 0.3,
        "total_amount" : 17.8,
        "tpep_pickup_datetime" : ISODate("2015-01-10T20:33:38.000Z"),
        "tpep_dropoff_datetime" : ISODate("2015-01-10T20:53:28.000Z")
    },
    "geometry_do" : {
        "type" : "Point",
        "coordinates" : [
            -73.9944152832031,
            40.7591094970703
        ]
    }
}

/* 3 */
{
    "_id" : ObjectId("5aa8d5b22d514514d4f95c0a"),
    "type" : "Feature",
    "geometry_pk" : {
        "type" : "Point",
        "coordinates" : [
            -73.9633407592773,
            40.8027877807617
        ]
    },
    "properties" : {
        "ID_Postgres" : 3,
        "VendorID" : 1,
        "passenger_count" : 1,
        "store_and_fwd_flag" : "N",
        "RatecodeID" : 1,
        "trip_distance" : 1.8,
        "payment_type" : 2,
        "fare_amount" : 9.5,
        "extra" : 0.5,
        "mta_tax" : 0.5,
        "tip_amount" : 0.0,
        "tolls_amount" : 0.0,
        "improvement_surcharge" : 0.3,
        "total_amount" : 10.8,
        "tpep_pickup_datetime" : ISODate("2015-01-10T20:33:38.000Z"),
        "tpep_dropoff_datetime" : ISODate("2015-01-10T20:43:41.000Z")
    },
    "geometry_do" : {
        "type" : "Point",
        "coordinates" : [
            -73.9518203735352,
            40.8244132995605
        ]
    }
}

拾取点记录在" geometry_pk"属性和下降点记录在" geometry_do"下。

我需要查询每个文件的接送点和下降点之间的距离(即出租车行程)。

1 个答案:

答案 0 :(得分:0)

您可以在代码中查询嵌入的坐标字段并计算距离:

db.collection.find({
    _id: your_id
}, {
    _id: 1,
    "geometry_pk.coordinate": 1,
    "geometry_do.coordinate": 1
});