[英]Can the field names in a MongoDB document be queried, perhaps using aggregation?
In this article from the MongoDB blog, " Schema Design for Time Series Data in MongoDB " the author proposed storing multiple time series values in a single document as numbered children of a base timestamp (ie document per minute, seconds as array of values). 在来自MongoDB博客的文章“ MongoDB 中的时间序列数据的架构设计 ”中,作者建议将多个时间序列值存储在单个文档中,作为基本时间戳记的编号子代(即,每分钟,秒作为值数组的文档)。
{
timestamp_minute: ISODate("2013-10-10T23:06:00.000Z"),
type: “memory_used”,
values: {
0: 999999,
…
37: 1000000,
38: 1500000,
…
59: 2000000
}
}
The proposed schema sounds like a good one but they fail to mention how to query the "values" field names which would be required if you wanted to know when the last sample occurred. 提议的模式听起来不错,但是它们没有提及如何查询“值”字段名称,如果您想知道最后一个样本何时发生,则需要使用该名称。
How would you go about constructing a query to find something like the time of the most recent metric (combining timestamp_minute and highest field name in the values)? 您将如何构造查询以查找诸如最新度量标准的时间(将timestamp_minute和值中的最高字段名称结合在一起)之类的内容?
Thanks so much! 非常感谢!
You can just query the minute document and then use a loop on the client to determine which timestamps have been set: 您可以只查询会议记录文档,然后在客户端上使用循环来确定已设置的时间戳记:
doc = c.find(...)
var last = 0
for (var i=0; i<60; i++)
if (i in doc.values)
last = i
Another approach which is a little more efficient is to use an array instead of a document for the per-second samples, and then use the length of the array to determine how many second samples have been stored: 另一种效率更高的方法是对每秒样本使用数组而不是文档,然后使用数组的长度确定已存储多少第二样本:
doc = c.find(...)
last = doc.values.length - 1
I found the answer "can the field names be queried" in another blog post which showed iterating over the keys (as Bruce suggests) only doing so in a MapReduce function ala: 我在另一篇博客文章中找到了“可以查询字段名称”的答案,该文章仅在MapReduce函数ala中显示了对键的迭代(如Bruce所建议的):
var d = 0;
for (var key in this.values)
d = Math.max(d, parseInt(key));
For the MMS example schema (swapping in month for timestamp_minute and days in the values array labeled v below) here is the data and a query that produces the most recent metric date: 对于MMS示例架构(以下标记为v的值数组中的timestamp_minute和days交换为月份),这是数据和查询,它们产生最近的度量标准日期:
db.metricdata.find();
/* 0 */
{
"_id" : ObjectId("5277e223be9974e8415f66f6"),
"month" : ISODate("2013-10-01T04:00:00.000Z"),
"type" : "ga-pv",
"v" : {
"10" : 57,
"11" : 49,
"12" : 91,
"13" : 27,
...
}
}
/* 1 */
{
"_id" : ObjectId("5277e223be9974e8415f66f7"),
"month" : ISODate("2013-11-01T04:00:00.000Z"),
"type" : "ga-pv",
"v" : {
"1" : 145,
"2" : 51,
"3" : 63,
"4" : 29
}
}
And the map reduce function: 和图归约功能:
db.metricdata.mapReduce(
function() {
var y = this.month.getFullYear();
var m = this.month.getMonth();
var d = 0;
// Here is where the field names used
for (var key in this.v)
d = Math.max(d, parseInt(key));
emit(this._id, new Date(y,m,d));
},
function(key, val)
{
return null;
},
{out: "idandlastday"}
).find().sort({ value:-1}).limit(1)
This produces something like 这会产生类似
/* 0 */
{
"_id" : ObjectId("5277e223be9974e8415f66f7"),
"value" : ISODate("2013-11-04T05:00:00.000Z")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.