logo
down
shadow

MongoDB: Using $geoIntersects or $geoWithin with $near in one query


MongoDB: Using $geoIntersects or $geoWithin with $near in one query

By : user2954278
Date : November 22 2020, 01:01 AM
around this issue This requires an aggregate pipeline. As per mogodb doc for $geoNear, You can only use $geoNear as the first stage of a pipeline. The aggregate function has an entry for an additional query which is where the polygon query will be used to narraw down results based on inclusion in the PLAYDATE_RANGE field of the document.
code :
db.friends.aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [ -73.98652 , 40.752044 ] },
        maxDistance: 300,
        distanceField: "friends.calculated_distance",
        query: {
       "PLAYDATE_RANGE":{
          "$geoIntersects":{
             "$geometry":{
                "type":"Point",
                "coordinates":[
                   -73.98652,
                   40.752044
                ]
             }
          }
       }
    },
        spherical: true

     }
   }
])
{ "_id" : "objid", "FRIEND_NAME" : "Bobby", "GEOMETRY" : {"type":"Polygon","coordinates":[[[-73.98779153823898,40.718233223261],[-74.004946447098,40.723575517498],[-74.006771211624,40.730592217474],[-73.99010896682698,40.746712376146],    [-73.973135948181,40.73974615047701],[-73.975120782852,40.736128627654],[-73.973997695541,40.730787341083],[-73.983317613602,40.716639396436],[-73.98779153823898,40.718233223261]]]}, "FRIEND_POSITION" : { "type" : "Point", "coordinates" : [ -73.992188, 40.729359 ] } }


Share : facebook icon twitter icon
MongoDB can't parse query (2dsphere): $geoWithin:

MongoDB can't parse query (2dsphere): $geoWithin:


By : michael ou
Date : March 29 2020, 07:55 AM
around this issue The polygon that you are providing for $geowithin query is incorrect. A polygon needs to have the same start and end point as per GeoJSON definition.
The correct query is:
code :
db.myCollection.find( { 'location.geometry': 
                           { '$geoWithin': 
                                    { '$geometry' :
                                          { 'type' : "Polygon", 
                                             'coordinates' :  [ 
                                                   [ -118.108006, 34.046072], 
                                                   [ -117.978230, 34.041521], 
                                                   [ -117.987328,33.913645 ],
                                                   [ -118.108006, 34.046072]
                                                ]  
                                          } 
                                     }
                             }
                       }
                     );
Difference in $geoWithin and $geoIntersects operators?

Difference in $geoWithin and $geoIntersects operators?


By : user3558815
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , From $geoIntersects:
code :
> db.test.drop()
> var poly1 = { "type" : "Polygon", "coordinates" : [[[0, 0], [3, 0], [0, 3], [0, 0]]] }
> var poly2 = { "type" : "Polygon", "coordinates" : [[[1, 1], [2, 1], [1, 2], [1, 1]]] }
// poly1 is a similar triangle inside poly2
> var poly3 = { "type" : "Polygon", "coordinates" : [[[1, 0], [-2, 0], [1, 3], [1, 0]]] }
// poly3 is poly1 flipped around its "vertical" edge, then bumped over one unit, so it intersects poly1 but is not contained in it
> db.test.insert({ "loc" : poly2 })
> db.test.insert({ "loc" : poly3 })
> db.test.ensureIndex({ "loc" : "2dsphere" })
> db.test.find({ "loc" : {
    "$geoIntersects" : {
        "$geometry" : poly1
    }
} })
// poly2 and poly3 returned
> db.test.find({ "loc" : {
    "$geoWithin" : {
        "$geometry" : poly1
    }
} })
// poly2 returned
Do geospatial queries work on arrays? ( $geoWithin, $geoIntersects )

Do geospatial queries work on arrays? ( $geoWithin, $geoIntersects )


By : Yang
Date : March 29 2020, 07:55 AM
I hope this helps .
This is one of those both yes and no questions to answer, as yes an array is supported for matching results in but it is also probably not what you really want considering the restrictions on how the matching is done.
code :
{
    "regions": [
        {
            "name": "penta",
            "geometry": {
                "type": "Polygon",
                "coordinates": [[
                    [ 
                        -77.0322804898023610, 
                        -12.1271067552781560
                    ], 
                    [ 
                        -77.0336792618036270, 
                        -12.1255133434450870
                    ], 
                    [ 
                        -77.0326449349522590, 
                        -12.1239143495252150
                    ], 
                    [ 
                        -77.0300991833209990, 
                        -12.1238251884504540
                    ], 
                    [ 
                        -77.0299865305423740, 
                        -12.1262000752832540
                    ], 
                    [ 
                        -77.0322804898023610, 
                        -12.1271067552781560
                    ]
                ]]
            }
        },
        {
            "name": "triangle",
            "geometry": {
                "type": "Polygon",
                "coordinates": [[
                    [ 
                        -77.0313568040728570, 
                        -12.1266573492018090
                    ], 
                    [ 
                        -77.0325788855552670, 
                        -12.1246968022373030
                    ], 
                    [ 
                        -77.0300653204321860, 
                        -12.1246233756874440
                    ], 
                    [ 
                        -77.0313568040728570, 
                        -12.1266573492018090
                    ]
                ]]
            }
        }
    ]
}
db.regions.createIndex({ "regions.geometry": "2dsphere" })
db.regions.find({
    "regions.geometry" : { 
        "$geoIntersects" : { 
            "$geometry" : { 
                "type" : "Polygon" , 
                "coordinates" : [[
                    [ -77.02877718955278 , -12.123750122669545],
                    [ -77.03457042574883 , -12.123750122669545],
                    [ -77.03457042574883 , -12.12736341792724],
                    [ -77.02877718955278 , -12.12736341792724], 
                    [ -77.02877718955278 , -12.123750122669545]
                ]]
            }
        }
    }
})
db.shapes.find({
    "geometry" : { 
        "$geoIntersects" : { 
            "$geometry" : { 
                "type" : "Polygon" , 
                "coordinates" : [ [ 
                    [ -77.02877718955278 , -12.123750122669545],
                    [ -77.03457042574883 , -12.123750122669545],
                    [ -77.03457042574883 , -12.12736341792724],
                    [ -77.02877718955278 , -12.12736341792724], 
                    [ -77.02877718955278 , -12.123750122669545]
                ]]
            }
        }
    }
})
{
    "_id" : ObjectId("55f8d2fa66c2e7c750414b7a"),
    "name" : "penta",
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [[
            [
                    -77.03228048980236,
                    -12.127106755278156
            ],
            [
                    -77.03367926180363,
                    -12.125513343445087
            ],
            [
                    -77.03264493495226,
                    -12.123914349525215
            ],
            [
                    -77.030099183321,
                    -12.123825188450454
            ],
            [
                    -77.02998653054237,
                    -12.126200075283254
            ],
            [
                    -77.03228048980236,
                    -12.127106755278156
            ]
        ]]
    }
}
{
    "_id" : ObjectId("55f8d2fa66c2e7c750414b7b"),
    "name" : "triangle",
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [[
            [
                    -77.03135680407286,
                    -12.126657349201809
            ],
            [
                    -77.03257888555527,
                    -12.124696802237303
            ],
            [
                    -77.03006532043219,
                    -12.124623375687444
            ],
            [
                    -77.03135680407286,
                    -12.126657349201809
            ]
        ]]
    }
}
mongodb $geoIntersects or $geoWithin a point matches multiple polygons - sort by area?

mongodb $geoIntersects or $geoWithin a point matches multiple polygons - sort by area?


By : Shale.Xiong
Date : March 29 2020, 07:55 AM
I wish this help you So I solved this in a different way. Calculating the areas of geography objects should be done ahead of time, when populating the index.
I wrote code to calculate area and bounds for each polygon and insert in the collection, so a conventional sort can be used, along with its indexes.
code :
db.collection.find({
    loc: {$geoInteresects: {$geometry: {
    type: 'Point',
    coordinates: [lng, lat]
}}}).sort({'properties.area': 1})
MongoDB $geoWithin $centerSphere query

MongoDB $geoWithin $centerSphere query


By : Murad.BUmmos
Date : March 29 2020, 07:55 AM
This might help you The code is perfect! Just the coordinates value are not saved as Number. So the Schema should become:
code :
var schema = new Schema({
        title   : { type: String, default: "generic"},
        guide   : String,
        loc     : {
            type : {type: String, default:"Point"},
            coordinates : [ { Number } ]
        },
    }
);
"loc": {
        "coordinates": [
            34,
            22
        ],
        "type": "Point"
    },
"loc": {
        "coordinates": [
            "34",
            "22"
        ],
        "type": "Point"
    },
Related Posts Related Posts :
  • Why is mongorestore painfully slow?
  • Is it better to use multiple databases when you are managing independent sets of things in MongoDB?
  • Cannot authenticate into mongo, "auth fails"
  • Why do we need an 'arbiter' in MongoDB replication?
  • How to create tree like compound index in mongodb
  • Call stored function in mongodb
  • Mongodb update deeply nested subdocument
  • how do non-ACID RethinkDB or MongoDB maintain secondary indexes for non-equal queries
  • How to set "arbiterOnly" to an existing node in a replicaset, from MongoDB MMS portal?
  • [METEOR $addToSet]Cannot insert into object '$addToSet' is empty
  • How to increment a field in mongodb?
  • "For" loop with meteor
  • howto route with collection's param in iron router Meteor JS
  • Connect to MongoDB on Azure (from client)
  • Aggregating filter for Key
  • Powershell Mongodb Authentication
  • Consequences of using $unwind on nested arrays?
  • Set operation with condition
  • How to create database schema for multiuser application (No SQL)
  • MongoDB data files created in wrong directory while changing oplog size
  • mongodb , wildcard in $in
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org