我正在尝试在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中使用时出错了什么?
答案 0 :(得分:1)
我遇到了同样的问题。我通过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行
答案 1 :(得分:-1)