Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Near Query - Broken when maxDistance to be omitted. #10521

Closed
ainsleyclark opened this issue Jan 12, 2025 · 4 comments · Fixed by #10622
Closed

Near Query - Broken when maxDistance to be omitted. #10521

ainsleyclark opened this issue Jan 12, 2025 · 4 comments · Fixed by #10622
Assignees

Comments

@ainsleyclark
Copy link

Describe the Bug

Problem

It seems as if the near query does not work with a point field when trying to query documents.

A very similar problem was reported on Discord in December here.

Description

I want to query the top 4 closest centres based of a lattiude/longtitude; maxDistance should not apply as I just need the first 4. In the example below I'm using an afterRead hook to populate the centres.

Documentation

The documentation states:

  • In order to do query based on the distance to another point, you can use the near operator. When querying using the near operator, the returned documents will be sorted by nearest first.
  • For distance related to a Point Field comma separated as longitude, latitude, maxDistance in meters (nullable), <minDistance in meters (nullable).

It's not quite clear what should be passed in, but from the looks of things it should be null if maxDistance should be omitted.

Debugging

I have tried the following:

  • near: [coords[0], coords[1], null, 0] - Results in no results.
  • near: [coords[0], coords[1]] - Results in Obtaining nearby centres error: syntax error at or near ")",
  • near: "${coords[1]},${coords[0]},null,0", - Results in no results.
  • near: [coords[0], coords[1], 999999999999, 0] - Results in the same centres for every query, results seem spardic.

Issue

It seems null doesn't work when passing to the near field when maxDistance and minDistance is to be omitted.

Example:

import type { CollectionConfig } from 'payload'

export const Centres: CollectionConfig = {
    slug: 'centres',
    access: {
        read: () => true,
    },
    fields: [
        {
            name: 'location',
            type: 'group',
            fields: [
                {
                    name: 'coordinates',
                    type: 'point',
                    required: true,
                }
            ]
        },
        {
            name: 'nearbyCentres',
            label: 'Nearby Centres',
            type: 'relationship',
            relationTo: 'centres',
            virtual: true,
            hasMany: true,
            admin: {
                readOnly: false,
            },
            hooks: {
                afterRead: [
                    async ({data, req, depth}) => {
                        if (!data || depth !== 0) {
                            return;
                        }

                        const coords = data.location.coordinates;

                        try {
                            const centres = await req.payload.find({
                                req,
                                collection: 'centres',
                                limit: 4,
                                pagination: false,
                                where: {
                                    and: [
                                        {
                                            'location.coordinates': {
                                                //near: `${coords[0]},${coords[1]},null,0`,
                                                near: [
                                                    coords[0],
                                                    coords[1],
                                                    null,
                                                    0,
                                                ]
                                            },
                                        },
                                        {
                                            id: {
                                                not_equals: data.id,
                                            }
                                        },
                                    ]
                                },
                            });

                            return centres.docs;
                        } catch (err) {
                            req.payload.logger.error(`Obtaining nearby centres ${err}`);
                        }
                    }
                ]
            }
        },
    ],
    upload: true,
}

Link to the code that reproduces this issue

https://github.com/ainsleydev/payload-near-issue

Reproduction Steps

Clone the repo or use the example attached above.

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

Binaries:
  Node: 22.6.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 9.15.3
Relevant Packages:
  payload: 3.14.0
  next: 15.0.4
  @payloadcms/db-postgres: 3.14.0
  @payloadcms/graphql: 3.14.0
  @payloadcms/next/utilities: 3.14.0
  @payloadcms/plugin-cloud-storage: 3.14.0
  @payloadcms/plugin-form-builder: 3.14.0
  @payloadcms/plugin-nested-docs: 3.14.0
  @payloadcms/plugin-search: 3.14.0
  @payloadcms/plugin-seo: 3.14.0
  @payloadcms/richtext-lexical: 3.14.0
  @payloadcms/translations: 3.14.0
  @payloadcms/ui/shared: 3.14.0
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Dec  6 18:56:34 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6020
  Available memory (MB): 65536
  Available CPU cores: 12
@ainsleyclark ainsleyclark added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Jan 12, 2025
@ainsleyclark ainsleyclark changed the title Near Query - Broken Near Query - Broken when maxDistance to be omitted. Jan 12, 2025
@r1tsuu r1tsuu self-assigned this Jan 16, 2025
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jan 16, 2025
@r1tsuu r1tsuu closed this as completed in 46c1b37 Jan 21, 2025
@ainsleyclark
Copy link
Author

Thanks @r1tsuu 🤜 !

@ainsleyclark
Copy link
Author

Hey @r1tsuu just wanted to check, this hasn't been released yet has it?

Copy link
Contributor

🚀 This is included in version v3.19.0

Copy link
Contributor

This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants