includes seems to load every document in the collection

106 views
Skip to first unread message

Brian

unread,
Dec 30, 2011, 7:59:23 PM12/30/11
to Mongoid

Hi,

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('4efa72f40d3bf62eec0048e8'),
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)


Also, I have identity_map_enabled and this is mongoid 2.3.4.

Does anyone have an idea why this might be happenings? I only want it
to eager load the comments from the 10 posts. Thanks!

Durran Jordan

unread,
Dec 31, 2011, 6:19:43 AM12/31/11
to mon...@googlegroups.com
Can you open a Github issue for it please? I completely forgot about this: eager loading is not taking limit into account.

2011/12/31 Brian <barms...@gmail.com>

Brian

unread,
Jan 1, 2012, 5:59:36 PM1/1/12
to Mongoid
Sure thing, opened here:
https://github.com/mongoid/mongoid/issues/1542

Thanks for the awesome gem and amazing work! Mongoid is really a
thing of beauty.

On Dec 31 2011, 3:19 am, Durran Jordan <dur...@gmail.com> wrote:
> Can you open a Github issue for it please? I completely forgot about this:
> eager loading is not taking limit into account.
>
> 2011/12/31 Brian <barmstr...@gmail.com>
>
>
>
>
>
>
>
>
>
> > Hi,
>
> > 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'),
Reply all
Reply to author
Forward
0 new messages