Mongoid每个文档字段执行一个查询?

时间:2016-09-07 23:09:39

标签: ruby-on-rails mongodb mongoid

我正在查看API请求的日志,我看到同一个集合中同一项目的几个mongodb查询:

MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | STARTED | {"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('5789a7fb92ba744522000069'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | SUCCEEDED | 0.001166488s
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | STARTED | {"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('5789a7fb92ba744522000069'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | SUCCEEDED | 0.001271851s
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | STARTED | {"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('5789a7fb92ba744522000069'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | SUCCEEDED | 0.0011178170000000001s
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | STARTED | {"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('5789a7fb92ba744522000069'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | SUCCEEDED | 0.001772669s
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | STARTED | {"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('5789a7fb92ba744522000069'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | SUCCEEDED | 0.003027585s
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | STARTED | {"find"=>"fans", "filter"=>{"_id"=>BSON::ObjectId('577d410292ba742b6b00335f')}, "limit"=>-1}
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | SUCCEEDED | 0.000534343s
MONGODB | 127.0.0.1:27017 | opera_bounties_development.find | STARTED | {"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('5789a7fb92ba744522000069')}, "sort"=>{"created_at"=>-1}}

rack-mini-profiler还告诉我,我正在为每个字段运行查询:

app/models/bounty_group.rb:91:in `initiator_bounty'
app/views/api/bounty_groups/_show.jbuilder:12:in `_app_views_api_bounty_groups__show_jbuilder___4003829608010900196_70143133442560'
app/views/api/bounty_groups/index.jbuilder:11:in `block (4 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:10:in `block (3 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:9:in `block (2 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:8:in `block in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:6:in `_app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'

{:command_name=>"find", :database_name=>"opera_bounties_development", :command=>{"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('578d905e92ba741e17000036'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}, :request_id=>nil}   

app/models/bounty_group.rb:91:in `initiator_bounty'
app/views/api/bounty_groups/_show.jbuilder:13:in `_app_views_api_bounty_groups__show_jbuilder___4003829608010900196_70143133442560'
app/views/api/bounty_groups/index.jbuilder:11:in `block (4 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:10:in `block (3 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:9:in `block (2 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:8:in `block in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:6:in `_app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'

{:command_name=>"find", :database_name=>"opera_bounties_development", :command=>{"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('578d905e92ba741e17000036'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}, :request_id=>nil}      

app/models/bounty_group.rb:91:in `initiator_bounty'
app/views/api/bounty_groups/_show.jbuilder:14:in `_app_views_api_bounty_groups__show_jbuilder___4003829608010900196_70143133442560'
app/views/api/bounty_groups/index.jbuilder:11:in `block (4 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:10:in `block (3 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:9:in `block (2 levels) in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:8:in `block in _app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'
app/views/api/bounty_groups/index.jbuilder:6:in `_app_views_api_bounty_groups_index_jbuilder__3982354297104172694_70143133979520'

{:command_name=>"find", :database_name=>"opera_bounties_development", :command=>{"find"=>"concrete_bounties", "filter"=>{"bounty_group_id"=>BSON::ObjectId('578d905e92ba741e17000036'), "is_initiator"=>true}, "limit"=>-1, "sort"=>{"created_at"=>-1}}, :request_id=>nil}   

我误解了数据,还是实际发生了?如果这是一个实际的性能问题,我该如何解决?我以为我在我的视图中正确使用jbuilder,如下所示:

json.id                      bounty_group.id.to_s
json.challenge               bounty_group.action
json.status                  bounty_group.state
json.fail_condition          bounty_group.fail_condition
json.total_amount            bounty_group.amount

0 个答案:

没有答案