我有一个包含 300 万份文档的集合。每个文档有 40 个字段。字段如下所示。
{
"b_date" : "2016-04-05",
"d_date" : "2016-06-25",
"pos" : "MISC",
"origin" : "DXB",
"destination" : "HGA",
"pax" : 1,
"pax_1" : 2
},
{
"b_date" : "2016-04-05",
"d_date" : "2016-06-25",
"pos" : "MISC",
"origin" : "DXB",
"destination" : "HGA",
"pax" : 4,
"pax_1" : 5
},
{
"b_date" : "2016-04-05",
"d_date" : "2016-06-26",
"pos" : "MISC",
"origin" : "DXB",
"destination" : "HGA",
"pax" : 3,
"pax_1" : 3
}
现在我想得到 pax 的总和和 pax_1按分组 b_date , d_date , pos , origin , destination领域。
累计人数是 pos 的分组, origin , destination字段,但累积 pax 和 pax_1 应根据 b_date 的升序增加和 d_date .
Expected result is.
{
"_id.dep_date" : "2016-04-05",
"_id.sale_date" : "2016-06-25",
"_id.pos" : "MISC",
"_id.origin" : "DXB",
"_id.destination" : "HGA",
"value.pax" : 5,
"value.cumulative_pax":5,
"value.pax_1" : 7,
"value.cumulative_pax_1":7,
},
{
"_id.dep_date" : "2016-04-05",
"_id.sale_date" : "2016-06-26",
"_id.pos" : "MISC",
"_id.origin" : "DXB",
"_id.destination" : "HGA",
"value.pax" : 3,
"value.cumulative_pax":8,
"value.pax_1" : 3,
"value.cumulative_pax_1":10,
}
我的 mapReduce 代码
db.collection.mapReduce(
function() {
emit(
{
"pos" : this.pos,
"origin" : this.origin,
"destination" : this.destination,
'dep_date': this.d_date,
'sale_date': this.b_date,
},
{
'pax':this.pax,
'pax_1':this.pax_1,
}
);
}
,
function(key,values) {
paxt = 0;
paxt_1 = 0;
for (var i in values){
paxt += values[i].pax;
paxt_1 += values[i].pax_1;
}
return {'pax':paxt,
'pax_1':paxt_1,
};
}
,
{
'scope':{
'pos':'',
'origin':'',
'destination':'',
'dep_date': '',
'sale_date': '',
'result':{}
}
,
'finalize':function(key,value) {
if (pos != key.pos ||
origin != key.origin ||
destination != key.destination ||
){
result['pax'] = 0;
result['pax_1'] = 0;
result['cumulative_pax'] = 0;
result['cumulative_pax_1'] = 0;
}
result['pax'] += value.pax;
result['cumulative_pax'] = value.pax;
result['pax_1'] += value.pax_1;
result['cumulative_pax_1'] = value.pax_1;
pos = key.pos;
origin = key.origin;
destination = key.destination;
dep_date = key.dep_date;
sale_date = key.sale_date;
return result;
}
,
'out':'some_collection'
}
)
这张 map 减少了返回的预期值,但它花了很多时间,比如 3 个小时。是因为 'b_date' 和 'd_date' 是字符串格式的日期吗?或者如何优化。
聚合在 3 分钟内返回结果,但我无法通过使用聚合获得累积 pax。
最佳答案
Map Reduce代码,
db.collection.mapReduce(
function() {
emit(
{
"pos" : this.pos,
"origin" : this.origin,
"destination" : this.destination,
'dep_date': this.d_date,
'sale_date': this.b_date,
},
{
'pax':this.pax,
'pax_1':this.pax_1,
}
);
}
,
function(key,values) {
paxt = 0;
paxt_1 = 0;
for (var i in values){
paxt += values[i].pax;
paxt_1 += values[i].pax_1;
}
return {'pax':paxt,
'pax_1':paxt_1,
};
}
,
{
'scope':{
'pos':'',
'origin':'',
'destination':'',
'dep_date': '',
'sale_date': '',
'result':{}
}
,
'finalize':function(key,value) {
if (pos != key.pos ||
origin != key.origin ||
destination != key.destination ||
){
result['pax'] = 0;
result['pax_1'] = 0;
result['cumulative_pax'] = 0;
result['cumulative_pax_1'] = 0;
}
result['pax'] += value.pax;
result['cumulative_pax'] = value.pax;
result['pax_1'] += value.pax_1;
result['cumulative_pax_1'] = value.pax_1;
pos = key.pos;
origin = key.origin;
destination = key.destination;
dep_date = key.dep_date;
sale_date = key.sale_date;
return result;
}
,
'out':'some_collection'
}
)
关于mongoDB mapreduce 运行 3m 文档需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44783517/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'
是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://