Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-58604

Allow $geoNear 'near' argument to be a let variable

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 5.1.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Fully Compatible
    • Query Optimization 2021-07-26, QO 2021-09-06, Query Optimization 2021-08-09, QO 2021-09-20, QO 2021-08-23

      Currently, 'near' has to be a literal array (or GeoJSON object):

      > db.c.aggregate([ {$geoNear: {near: [0, 0]}} ]

      Instead we should allow it to be an expression:

      > db.c.aggregate([ {$geoNear: {near: "$$pt"}} ], {let: {pt: [0, 0]}})

      It doesn't make sense to allow the query point to vary per-document, so it has to be a constant expression.

      We also want to allow 'near' to be bound in a $lookup:

      {$lookup: {
        from: 'coll',
        as: 'docs',
        let: {pt: "$location"},
        pipeline: [
          {$geoNear: {
            near: "$$pt",

      The expression "$$pt" is constant within each subquery.

      To make this work I think we'll want to:

      • Change the DocumentSourceGeoNear parser to accept any expression, for 'near'.
      • Change DocumentSourceGeoNear::optimize to optimize the expression.
      • When we convert DocumentSourceGeoNear to DocumentSourceGeoNearCursor, raise an error if the expression is not a constant.

            milena.ivanova@mongodb.com Milena Ivanova
            david.percy@mongodb.com David Percy
            0 Vote for this issue
            4 Start watching this issue