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

Improve backing query when using aggregation helpers

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Associations, Query
    • None

      The given code

      class User
        include Mongoid::Document
      
        has_many :cgm_evgs
      end
      
      class CgmEvg
        include Mongoid::Document
      
        field :timestamp, type: Date
      
        belongs_to :user
      end
      
      User.find(_id: u1.id).cgm_evgs.min(:timestamp).to_a
      

      generates the following command:

      {
      	"aggregate":"cgm_evgs",
      	"pipeline":[
      		{"$match":{"user_id":{"$oid":"65a7dfcfe61d72729750604a"},"timestamp":{"$exists":true}}},
      		{
      			"$group":{
      				"_id":"timestamp",
      				"count":{"$sum":1},
      				"max":{"$max":"$timestamp"},
      				"min":{"$min":"$timestamp"},
      				"sum":{"$sum":"$timestamp"},
      				"avg":{"$avg":"$timestamp"}
      			}
      		}
      	]
      }
      

      The command does not reflect the code.

            Assignee:
            Unassigned Unassigned
            Reporter:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: