Sunday, 15 March 2015

mongodb - 2 geointersect queries with embedded documents -



mongodb - 2 geointersect queries with embedded documents -

i have collection called "sets" hundreds of documents similar 1 :

{ "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 ] ] ] } } ] }

type polygons connected 1 or multiple type b polygons. have been trying hours create next query :

user submits 2 points coordinates, , b. geointersect point "type a" polygons in collection. for each match, @ type "connectedto" array , geointersect connected type b polygons point b. return setnumber , matching recid pairs

for example, point = [3, 5] (geointersects recid = 1) point b = [-80, 30] (geointersects recid = 3) should return

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

any hint/ideas on how implement welcome !

thanks

edit: can modify documents fields/data model @ if needed

edit 2 next neil comment : query output specific subdocument

db.sets.aggregate([ {$unwind: "$rectangles"},{ $match: { "rectangles.geo": { $geointersects: { $geometry: {type: "point", coordinates: [3, 5]} } } } }, {$project: {"rectangles.connectedto": 1, "_id":0, "setnumber":1}} ])

the result :

{ "result" : [ { "setnumber" : 1, "rectangles" : { "recid" : 1, "connectedto" : [ 2, 3 ] } } ], "ok" : 1 }

mongodb

No comments:

Post a Comment