无法计算平均时间 [英] Unable to compute average time

查看:96
本文介绍了无法计算平均时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB已经3天大了,我觉得它不是很流畅.我只是试图计算一个字段的平均时间,但我一直遇到各种各样的问题.

3 days old into MongoDB, and I am not finding it very fluid. I am simply trying to compute the average time for a field but I keep running into all sorts of problems.

这是我的代码:

db.results.group({
     key:{"profile.Zend_Http_Client_Adapter_Socket::read==>fgets":{$exists:true}},
     initial: {count: 0, total:0},
     reduce: function(doc, out){ 
         out.count++; 
         out.total += doc."profile.Zend_Http_Client_Adapter_Socket::read==>fgets.wt";
     },
     finalize: function(out){
            out.avg = out.total/out.count;
     }
});

错误:

SyntaxError:意外的字符串

以上是一个非常幼稚的错误,但我不明白为什么会这样说,我想到的唯一可能的原因是,我在这里指定的键在引号中,因此Mongo感到困惑.

The above is a very childish error, but I can't understand why it would state this, the only plausible reason I can think of is that the keys I have specified here are in quotes hence Mongo is getting confused.

但是,这些键在我的收藏夹中用引号引起来,所以应该没有理由继续正确地显示此语法错误吗?

BUT, these keys ARE in quotes in my collection, so there should be no reason why I keep getting this syntax error right ?

示例文档:

{
    "_id" : ObjectId("532a2a986803faba658b456b"),
    "profile" : {
        "main()==>register_shutdown_function" : {
            "ct" : 1,
            "wt" : 13,
            "cpu" : 0,
            "mu" : 1568,
            "pmu" : 1000
        },
        "main()==>load::htdocs/index.php" : {
            "ct" : 1,
            "wt" : 17,
            "cpu" : 0,
            "mu" : 1736,
            "pmu" : 4296
},
        {"Zend_Http_Client_Adapter_Curl::write==>curl_exec" : {                                           
            "ct" : 3,
            "wt" : 54782314,
            "cpu" : 16001,
            "mu" : 83288,
            "pmu" : 49648
        }, ....

推荐答案

根据评论,您的问题是形成有效JavaScript之一.同样,您的关键"值似乎并不是您真正想要的.但是,您应该偏爱聚合函数使用组"

As per the comment your problem is one of forming valid JavaScript. Also your "key" value would not seem to be what you really want. There is however the aggregate function that you should be favoring over the use of "group"

db.results.aggregate([
    { "$match": {
        "$and": [
            { "profile.Zend_Http_Client_Adapter_Socket::read==>fgets.wt": {
                "$exists": true 
            }},
            { "profile.Zend_Http_Client_Adapter_Socket::read==>fgets.wt": { 
               "$not": { "$type": 2 }
            }}
        ]
    }},
    { "$group": {
        "_id": null,
        "total": { "$sum": 
            "$profile.Zend_Http_Client_Adapter_Socket::read==>fgets.wt"
        },
        "count": { "$sum": 1 }
    }},

    { "$project": {
        "_id": 0,
        "avg": { "$divide": [ "$total", "$count" ] }
   }}
])

聚合管道取代了以前引入的功能,例如groupdistinct.对于所有琐碎的操作, 应该是您的首选.

The aggregation pipeline sort of supercedes earlier introduced functions such as group and distinct. And for all but trivial operations should be your favored choice.

它将以更快的速度运行,并且将以本机代码而不是JavaScript引擎进行处理.

It will run much faster as well as this is processed in native code and not the JavaScript engine.

另请参见文档中的 SQL到聚合的映射图

您的样本不是很完整.要解决所有问题,我必须将这样的文档放进去:

Your sample is not very complete. To sort out all issues I have to put in a document like this:

{
    "profile": {
        "Zend_Http_Client_Adapter_Socket::read==>fgets": {                                           
            "ct" : 3,
            "wt" : 54782314,
            "cpu" : 16001,
            "mu" : 83288,
            "pmu" : 49648
        },
    }
}

您的文档示例中也包含一些无效字段:

Also your document example has some invalid fields in it:

{
    "_id" : ObjectId("532a2a986803faba658b456b"),
    "profile" : {
        "main()==>register_shutdown_function" : {
            "ct" : 1,
            "wt" : 13,
            "cpu" : 0,
            "mu" : 1568,
            "pmu" : 1000
        },
        "main()==>load::htdocs/index.php" : { <-- Invalid
            "ct" : 1,
            "wt" : 17,
            "cpu" : 0,
            "mu" : 1736,
            "pmu" : 4296
},

因为该字段的字段名称中有.,所以该字段不能存在,出于明显的子文档原因,不允许使用该字段.

So that field cannot exist as it has a . in the field name, which for obvious sub-document reasons is not allowed.

这篇关于无法计算平均时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆