mongoid/mongoid — Issue #1542: eager loading doesn't use limit
Author: barmstrong
Created: 2012-01-01 22:58:33+00:00
State: open

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!