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

eager loading doesn't use limit

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.0
    • Affects Version/s: None
    • Component/s: None

      By request, opening issue based on this thread:

      http://groups.google.com/group/mongoid/browse_thread/thread/8ac8ae4851c1b3ab

      The issue is that eager loading doesn't seem to respect the limit (and eager loads all associations?)

      For example, I have a fairly standard has_many/belongs_to relationship.

      class Post 
        include Mongoid::Document 
        has_many :comments 
        ... 
      end 
      class Comment 
        include Mongoid::Document 
        belongs_to :post 
        ... 
      end
      

      And when I do a query like this:

      Post.includes(:comments).limit(10)

      It seems to load every comment in a first query, before getting posts. (not just for the 10 posts)

      MONGODB myapp_development['system.namespaces'].find({})
      MONGODB myapp_development['posts'].find({},

      {"_id"=>1}

      )
      MONGODB cursor.refresh() for cursor 8721222270629761565
      MONGODB myapp_development['system.namespaces'].find({})
      MONGODB
      myapp_development['comments'].find({"post_id"=>{"$in"=>[BSON::ObjectId('4ef a72f40d3bf62eec0048e8'),
      BSON::ObjectId('4efe1ecb0d3bf686b600000a')
      ...
      (1000's of lines of every post_id in giant array)
      })

      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.refresh() for cursor 8977681154490296445
      MONGODB cursor.close 2744030960413394617
      MONGODB myapp_development['posts'].find({}).limit(30)

      Tested with identity_map_enabled and mongoid 2.3.4.

      Thanks!

            Assignee:
            Unassigned Unassigned
            Reporter:
            barmstrong Brian Armstrong
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: