Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Calculate distance using geoNear with lookup

I am trying to calculate distance using geoNear based on value from lookup pipeline.

For this I tried as:

db.bookDetail.aggregate([
    {
        $geoNear:{
            near: {
                type:'Point',
                coordinates[70.12,20.12]
            },
            distanceField: 'distance',
            key: 'bookcenter.address',
            spherical: true
         }
    },
    {
        $lookup:{
            from: 'books',
            localField:'book.id',
            foreignField: '_id',
            as: 'bookcenter'
        }
    }
])

It gives the bad Value error: MongoError: error processing query

And geoNear cannot be used after lookup , as I need value from lookup to pass to the key of geoNear.

Please let me know how can I calculate the distance.

If anyone needs any further information please let me know.

Note: I have latitude and longitude and want to calculate the distance from address present in books collection which is fetched using lookup

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The $geoNear field was designed to efficiently locate point using a geospatial index. Using it to just calculate distance is a lot of unnecessary overhead.

If you aren't filtering by distance, it will be more efficient to calculate the distance on the client size with a library such as https://www.npmjs.com/package/geographiclib

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!