mongodb - 2个嵌入式文档的geointersect查询

时间:2014-06-19 22:00:34

标签: mongodb

我有一个名为" sets"有数百个与此类似的文件:

{
"setNumber": "1",
"rectangles": [
    {
        "RecId" : 1,
        "RecType" : "A",
        "connectedTo" : [2,3],
         "title": "Nigeria",
         "geo": {
            "type": "Polygon",
            "coordinates": [
                [
                    [ 0 , 0 ] ,
                    [ 3 , 10 ] ,
                    [ 10 , 1 ] ,
                    [ 0 , 0  ]
                ]
            ]
        }
    },
    {
        "RecId" : 2,
        "RecType" : "B",
        "title": "Europe",
        "geo": {
            "type": "Polygon",
            "coordinates": [
            [
                [ 45 , 45 ] ,
                [ -10 , 60 ] ,
                [ -10 , 40 ] ,
                [ 45 , 45  ]
            ]
        ]
        }
    },
    {
        "RecId" : 3,
        "RecType" : "B",
        "title": "North America",
        "geo": {
            "type": "Polygon",
            "coordinates": [
                [
                    [ -65 , 20 ] ,
                    [ -140 , 20 ] ,
                    [ -140 , 50 ] ,
                    [ -65, 50 ],
                    [ -65 , 20  ]
                ]
            ]
        }
    },
    {
        "RecId" : 4,
        "RecType" : "A",
        "connectedTo" : [3],
        "title": "Brazil",
        "geo": {
            "type": "Polygon",
            "coordinates": [
                [
                    [ -30 , -30 ] ,
                    [ -100 , -30 ] ,
                    [ -100 , 0 ] ,
                    [ -30, 0 ],
                    [ -30 , -30  ]
                ]
            ]
        }
    }
    ]
}

A型多边形连接到一个或多个B型多边形。 我一直在努力创建以下查询:

  1. 用户提交2个点坐标,A和B.
  2. GeoIntersect A点,所有"类型A"集合中的多边形。
  3. 对于每场比赛,请查看类型A" connectedTo" array和geoIntersect连接类型B多边形与点B。
  4. 返回setNumber和匹配的RecId对
  5. 例如,

    点a = [3,5](与RecId = 1的geoIntersects)
    点b = [-80,30](与RecId = 3的geoIntersects)
    应该返回

    {
    "setNumber":"1",
    "results": [[1,3]]
    }
    

    非常欢迎任何关于如何实现这一点的提示/想法!

    由于

    编辑:如果需要,我可以随意修改文档字段/数据模型

    在Neil评论后编辑2:
    此查询输出特定的子文档

    db.sets.aggregate([
        {$unwind: "$rectangles"},{
            $match: {
                "rectangles.geo": {
                    $geoIntersects: {
                        $geometry: {type: "Point", coordinates: [3, 5]}
                    }
                }
            }
        },
        {$project: {"rectangles.connectedTo": 1, "_id":0, "setNumber":1}}
        ])
    

    结果是:

    {
        "result" : [
            {
                "setNumber" : 1,
                "rectangles" : {
                                "RecId" : 1,
                    "connectedTo" : [
                        2,
                        3
                    ]
                }
            }
        ],
        "ok" : 1
    }
    

0 个答案:

没有答案