简体   繁体   English

是否可以使用聚合查询MongoDB文档中的字段名称?

[英]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.

相关问题 使用 mongodb 聚合将文档合并到数组字段 - Merge document to an array field using mongodb aggregation MongoDB 聚合 - 保留字段名称 - MongoDB Aggregation - Keeping field names [mongodb],如何通过聚合获得具有许多不同字段的文档的最大值? 子字段名称相同 - [mongodb], How can I get maximum value of a document with many different fields through aggregation? sub field names are same 使用聚合根据日期字段将一个MongoDB文档分成多个文档 - Divide a MongoDB document into multiple documents based on Date Field using aggregation 使用MongoDB聚合在数字字段上返回具有最大值的文档 - Return a document with the max value on a number field using MongoDB aggregation 使用聚合增加或减少每个文档 mongodb 中的一个字段 - increment or subtract a field in every document mongodb using aggregation MongoDB 文档的数组字段的每个元素的聚合 - MongoDB Aggregation for every element of array field of a document 使用聚合管道创建基于子文档值的名称的MongoDB字段? - Create MongoDB fields with names based on sub-document values using aggregation pipeline? Mongodb聚合:使用同一文档上另一个字段的累加器获取字段值 - Mongodb aggregation: get field value using accumulator of another field on the same document 如何建立将父文档字段与嵌入式数组文档字段相关的查询。 使用MongoDB聚合运算符 - How to build query for relating parent document's field to embedded array document's field. Using MongoDB aggregation Operators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM