我在Mongo中存储了一些包含任意字段的数据。此字段存储为字符串,但可以包含二进制数据,整数,浮点数或其他任何内容(直到应用程序和另一个字段,“类型”)。无论如何,假设我确定我正在查询的文档在此字段中存储浮动值键入字符串,我想知道这些值在特定日期范围内的平均值。不幸的是,根据$avg documentation,它会忽略任何非数字值。有没有办法强制parseFloat或类似的东西在运行时转换文档值来安抚$ avg运算符?
示例文档结构:
{
"_id": ObjectId("540f4e29f287300b9097663d"),
"DateReceived": ISODate("2014-09-09T18:59:53.717Z"),
"Message": {
"Version": 1,
"Source": {
"Device": "MS02166",
"Application": "Performance Counter",
"Category": "Disk - Reads/sec",
"OriginDate": ISODate("2014-09-09T18:59:51.256Z"),
"Level": 2
},
"PayloadText": "0"
}
}
我平均的字段为Message.PayloadText
(在此示例中为0,但文档非零)
我能够生成的聚合管道查询是:
[{
"$match": {
"Message.Source.Application": "Performance Counter"
}
}, {
"$match": {
"Message.Source.Category": "Processor"
}
}, {
"$match": {
"Message.Source.Level": 2
}
}, {
"$match": {
"Message.Source.Device": "MS02166"
}
}, {
"$match": {
"DateReceived": {
"$gte": ISODate("2014-09-14T07:00:00Z"),
"$lt": ISODate("2014-09-15T07:00:00Z")
}
}
}, {
"$group": {
"_id": {
"hour": {
"$hour": "$DateReceived"
}
},
"AvgUsage": {
"$avg": "$Message.PayloadText"
},
"MinUsage": {
"$min": "$Message.PayloadText"
},
"MaxUsage": {
"$max": "$Message.PayloadText"
}
}
}]
我尝试过像"$avg": "parseFloat($Message.PayloadText)"
和"$avg": "$parseFloat($Message.PayloadText)"
这样的事情无济于事。奇怪的是,$ min和$ max运算符产生了一个有意义的结果(下面的样本结果)。
{ "_id" : { "hour" : 7 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 8 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 9 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 10 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.858578" }
{ "_id" : { "hour" : 11 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.999424" }
{ "_id" : { "hour" : 12 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.999424" }
{ "_id" : { "hour" : 21 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.999424" }
{ "_id" : { "hour" : 16 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 15 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 23 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 18 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "94.02982" }
{ "_id" : { "hour" : 0 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 20 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 22 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "98.48484" }
{ "_id" : { "hour" : 13 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 17 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "98.76543" }
{ "_id" : { "hour" : 14 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "97.18308" }
{ "_id" : { "hour" : 19 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 1 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
{ "_id" : { "hour" : 4 }, "AvgUsage" : 0, "MinUsage" : "0", "MaxUsage" : "9.37442" }
答案 0 :(得分:5)
如果您想要平均值,则需要存储一个数字。没有任何类型转换函数可以让您从数字的字符串表示映射到数字。 $min
和$max
有效,因为在每个BSON类型(包括字符串)和不同的BSON类型之间定义了一个顺序,所以MongoDB总能找到任何字段的最大值。