Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5096

Calling "count" on query with $near results in bad aggregation

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      When calling "count" on a Criteria that includes $near, I get the error, "Mongo::Error::OperationFailure Exception: $geoNear, $near, and $nearSphere are not allowed in this context (2)".

      I noticed this issue when upgrading from Mongoid 7.0.2 to 7.2.2. Previously, the count was returned correctly.

      A behavior difference I notice is that with mongoid 7.2.2 the query is turned into an aggregation. Previously, this was a regular count command.

      Example aggregation sent to the server:

      {
        aggregate: "college_listings",
        pipeline: [
          {
            $match: {
              active_in_search: true,
              loc: {$near: [20, 20]},
              $and: [{loc: {$near: [20, 20]}}]
            }
          },
          {$group: {_id: 1, n: {$sum: 1}}}
        ],
        cursor: {},
        $db: "raise-test",
        lsid: {id: UUID("0fc75d31-449b-4fd6-ad70-1284c0b57808")}
      }
      

      Ruby code:

      class CollegeListing
        include Mongoid::Document
      
        field :loc, type: Array
      end
      
      CollegeListing.where(loc: { '$near' => [20, 20] }).count
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            luke.lovett@campuslogic.com Luke Lovett
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: