我正在尝试在 Node.js 中执行此 MongoDB 命令:
db.events.group({
key: {
'timestamp.d': true
},
cond: {
'event_name': 'search'
},
initial: {
'count': 0,
'empty': 0,
'redos': 0
},
reduce: function(item, summaries) {
summaries.count++;
if (item.result_count == 0) {
summaries.empty++;
}
if (item.original_query) {
summaries.redos++;
}
var totalSuccesses = (summaries.count - summaries.redos - summaries.empty);
summaries.percentNonFailures = (totalSuccesses / summaries.count) * 100
}
})
这在 Mongo 命令中非常有效,就像每天给我摘要一样。当我在 Node.js 中尝试这个时:
db.collection('events', function(err, collection) {
collection.group({
"timestamp.d": true
}, {
"event_name": "search"
}, {
count: 0,
empty: 0,
redos: 0,
percentNonFailure: 0,
}, function(item, summaries) {
summaries.count++;
if (item.result_count == 0) {
summaries.empty++;
}
if (item.original_query) {
summaries.redos++;
}
totalSuccesses = summaries.count - summaries.redos - summaries.empty
summaries.percentNonFailure = (totalSuccesses / summaries.count) * 100
}, function(err, results) {
self.eventEmitter.emit(doneEvent, results)
});
});
我得到一个单一的结果,将每天的所有总数加在一起,所以基本上是整个期间的计数、空、重做的总和。
尝试将查询转换为在 Node.js 中使用时做错了什么?
最佳答案
我遇到了同样的问题。我深入研究了 mongodb 驱动程序代码。
问题是来自子文档的键。司机最终得到类似的东西
obj["timestamp.d"]
这不是你想要的。
我发现的是解决了我的问题的“命令”参数。
db.collection('events', function(err, collection){
collection.group(
[ "timestamp.d" ], // array element, might also work the old way
{ "event_name": "search" },
{
count: 0,
empty: 0,
redos: 0,
percentNonFailure: 0,
},
function(item, summaries){
summaries.count++;
if (item.result_count == 0) { summaries.empty++; }
if (item.original_query) { summaries.redos++; }
totalSuccesses = summaries.count - summaries.redos - summaries.empty
summaries.percentNonFailure = (totalSuccesses / summaries.count) * 100
},
true, // use the group command
function(err, results){ self.eventEmitter.emit(doneEvent, results) }
);
})
你可以在这里看到: collection.js 从第 1054 行开始
关于mongodb - 在 Node.js 中执行 MongoDB 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3428246/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption