Skip to content

use column value as comparison in query #2913

Closed

Description

@florianbepunkt

Is it possible to use column values for comparison in queries?

Background: I have some kind of dating app, using parse-server as backend. Each user has two fields: a radius field and a geopoint field. In radius field users store in which radius they are looking for other users. I want to to do a two way geo query now…show users other users based on their location and radius setting.

Example: User A and User B are 5 miles apart. User A has a radius he wants to search of 10 miles, user B only has a radius of 1 mile. If user a does a query he should get no results, since user b is within his radius, but he himself (user a) is not within user b's radius.

A one-way check, to see if user B is within user a's radius is simple:
query.withinKilometers('lastLocation', user.get('lastLocation'), user.get('lookingForRadius'));

But I struggle to to the second check to see whether user a is also within user B's radius. For this I would nee to use a column value for comparison in my query.

Pseudo-code would be this
query.withinKilometers('lastLocation', user.get('lastLocation'), 'userRadiusColumnValue');

where userRadiusColumnValue would not be a fixed value but a column value that would change for each row that is queried.

Is this possible in Parse-server? If not, is there another approach to these scenarios where you would need to compare column values in a query?

Activity

  1. bohemima commented on Oct 23, 2016

    @bohemima
    Contributor

    General questions on how to use the SDKs doesnt really belong here, try to keep this for actual bug-reports and/or feature requests.

    You are probably looking for matchesKeyInQuery

  2. florianbepunkt commented on Oct 23, 2016

    @florianbepunkt
    Author

    No, I am not looking for matchesKeyInQuery. Mh… not sure about the place to ask. The documentation refers to Google Group, where it reads that the group is only for archive purposes and instead they refer to Github. Feels like circular dependency injection…

  3. AmbroiseCollon commented on Oct 24, 2016

    @AmbroiseCollon

    You should do a cloud code function that take care of this for you. Like this :

    Parse.Cloud.define('getCloseUsers', function (request, response) {
        var user = request.user
        var query = new Parse.Query(Parse.User)
        query.withinKilometers('lastLocation', user.get('lastLocation'), user.get('lookingForRadius'))
        query.find({
            success: function(foundUsers) {
                var results = []
                for (var i =  0; i < foundUsers.length; i++) {
                    var aUser = foundUsers[i]
                    var distance = // Calculate distance between aUser last location and current user last location 
                    if (distance < aUser.get('lookingForRadius')) {
                        results.append(aUser)
                    }
                }
                response.success(results)
            },
            error: function(foundUsers, error) {
                response.error('Unable to query users with error: ' + error.message)
            }
        }
        )
    })
    

    Hope it helps. Btw, a good place for this question would be SO.

  4. florianbepunkt commented on Oct 24, 2016

    @florianbepunkt
    Author

    Thank you, this is much appreciated.

    Regarding the distance calculation. Can I access the distance calculation methods that must exists somewhere in parse server or use haversine formular myself.

    Again, thank you.

  5. danibjor commented on Oct 24, 2016

    @danibjor

    You could use the methods on the ParseGeoPoint class.

    var dist = myGeoPoint.kilometersTo(otherGeoPoint)

    dokumented here

  6. danibjor commented on Oct 25, 2016

    @danibjor

    You could use the methods on the ParseGeoPoint class.
    var dist = myGeoPoint.kilometersTo(otherGeoPoint)
    https://github.com/ParsePlatform/Parse-SDK-JS/blob/master/src/ParseGeoPoint.js#L147

    -------- Opprinnelig melding --------Fra: florianbepunkt [email protected] Dato: 24.10.2016 17:31 (GMT+01:00) Til: ParsePlatform/parse-server [email protected] Emne: Re: [ParsePlatform/parse-server] use column value as comparison in query (#2913)
    Thank you, this is much appreciated.

    Regarding the distance calculation. Can I access the distance calculation methods that must exists somewhere in parse server or use haversine formular myself.

    Again, thank you.

    —
    You are receiving this because you are subscribed to this thread.
    Reply to this email directly, view it on GitHub, or mute the thread.

    {"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/ParsePlatform/parse-server","title":"ParsePlatform/parse-server","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/ParsePlatform/parse-server"}},"updates":{"snippets":[{"icon":"PERSON","message":"@florianbepunkt in #2913: Thank you, this is much appreciated. \r\n\r\nRegarding the distance calculation. Can I access the distance calculation methods that must exists somewhere in parse server or use haversine formular myself.\r\n\r\nAgain, thank you."}],"action":{"name":"View Issue","url":"https://github.com/ParsePlatform/parse-server/issues/2913#issuecomment-255775101"}}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions