Home > Mobile >  How do we reverse coordinates before geo query in Mongo database?
How do we reverse coordinates before geo query in Mongo database?

Time:01-08

We have a system that has stored location coordinates in the incorrect order. There I'm unable to perform geo queries such as the ones below.

db.getCollection("users").find( 
{  "$and" : [
            {
                "datetime" : {
                    "$gte" : ISODate("2022-12-25T00:00:00.126 0000")
                }
            },
            {
                "datetime" : {
                    "$lte" : ISODate("2023-01-27T11:00:00.126 0000")
                }
            },
            {
                "user_id" : {
                    "$in" : [
                        "a2UxA9ysCQlk8XdYNToT5"
                    ]
                }
            },
            { 
                location: {
                    $geoIntersects: {
                       $geometry: {
                         type: "Polygon",
                            coordinates: [[
                                [
                                    55.04656085673112,
                                    24.97153076104319
                                ],
                                [
                                    55.05205402079362,
                                    24.97545995358559
                                ],
                                [
                                    55.057633015544596,
                                    24.974176171805773
                                ],
                                [
                                    55.06005773249406,
                                    24.971958698993816
                                ],
                                [
                                    55.05246171656388,
                                    24.96530604075661
                                ],
                                [
                                    55.04656085673112,
                                    24.97153076104319
                                ]
                            ]]
                        }
                    }
                }
            }
        ]
})

What I am thinking is, I need to reverse the coordinates before performing this query, I find out that there's something called $reverseArray but it's always applied on the $project object.

Eg:

db.collection.aggregate(
    [ 
        { "$project": { "location": { "$reverseArray": "$location" } } }
    ]
)

So I need help to find out if there is any workaround here. Due to a few reasons, we're not able to rectify the order of the geo-coordinates in our database.

Here's the snippet of DB stored value:

enter image description here

My main goal here is to find out whether the location points are within the polygon or not. Since we have stored the coordinates in the wrong order, we're not able to perform the initially mentioned above query.

CodePudding user response:

I found the query and here it is,

db.getCollection("users").aggregate([
  {
    $project: {
      location: { $reverseArray: "$location.coordinates" }
    }
  },
  {
    $match: {
      location: {
        $geoIntersects: {
           $geometry: {
             type: "Polygon",
                coordinates: [[
                    [
                        55.04656085673112,
                        24.97153076104319
                    ],
                    [
                        55.05205402079362,
                        24.97545995358559
                    ],
                    [
                        55.057633015544596,
                        24.974176171805773
                    ],
                    [
                        55.06005773249406,
                        24.971958698993816
                    ],
                    [
                        55.05246171656388,
                        24.96530604075661
                    ],
                    [
                        55.04656085673112,
                        24.97153076104319
                    ]
                ]]
            }
        }
      }
    }
  }
])
  • Related