From 67aa6200486eef9b02234336f537d5c80278d9d3 Mon Sep 17 00:00:00 2001 From: Eugeniy Belyaev Date: Tue, 27 Oct 2015 18:31:24 +0300 Subject: [PATCH 1/2] Added hint to use filename index In some cases mongodb query planner does not use filename index and scans by uploadDate which much slower. Using hint we can ensure that document will be looked up by filename index. --- lib/mongoid/grid_fs.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/mongoid/grid_fs.rb b/lib/mongoid/grid_fs.rb index 60ed44a..339446f 100644 --- a/lib/mongoid/grid_fs.rb +++ b/lib/mongoid/grid_fs.rb @@ -204,8 +204,9 @@ def [](filename) file_model. where(:filename => filename.to_s). order_by(:uploadDate => :desc). - limit(1). - first + extras(:hint => { :filename => 1 }). + limit(1). + first end def []=(filename, readable) From 884de444a27a1ed55c64aa7bebaaa26d6a698a0f Mon Sep 17 00:00:00 2001 From: Evgeniy Belyaev Date: Tue, 4 Feb 2020 14:25:16 +0100 Subject: [PATCH 2/2] Ruby hash style --- lib/mongoid/grid_fs.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoid/grid_fs.rb b/lib/mongoid/grid_fs.rb index 79f357b..9acbe1f 100644 --- a/lib/mongoid/grid_fs.rb +++ b/lib/mongoid/grid_fs.rb @@ -186,7 +186,7 @@ def [](filename) file_model .where(filename: filename.to_s) .order_by(uploadDate: :desc) - .extras(:hint => { :filename => 1 }) + .extras(hint: { filename: 1 }) .limit(1) .first end