简体   繁体   English

从JSON对象数组中的字段中提取值

[英]Extract values from fields in an array of JSON objects

I have a collection in MongoDB with many documents that have the same structure. 我在MongoDB中有一个集合,其中包含许多具有相同结构的文档。

When I perform a find query with some filters over the collection with this code: 当我使用以下代码对集合中的一些过滤器执行查找查询时:

Comment.find(
        {
            "data.time.s": {
                "$gte": "2019-05-13",
                "$lte": "2019-05-27"
            }
        },
        {
            "data.iaqi.co.v": "$data.iaqi.co.v",
            "data.time.s": "$data.time.s",
            "_id": 0

        }, function (error, datos) {

            console.log(datos)

        });

I get the following output when I console.log(datos): 当我console.log(datos)时,得到以下输出:

[
  { 
    "data" : {
        "iaqi" : {
            "co" : {
                "v" : 3.2
            }
        }, 
        "time" : {
            "s" : "2019-05-14 12:00:00"
        }
    }
},
{ 
    "data" : {
        "iaqi" : {
            "co" : {
                "v" : 4.7
            }
        }, 
        "time" : {
            "s" : "2019-05-15 00:00:00"
        }
    }
  }
]

The array contains more than 2 objects, but for the purpose of the example I just put 2 of them. 该数组包含两个以上的对象,但是出于示例的目的,我仅放置了其中的两个对象。

All of the objects in the array have the same structure, just the values of "v" and "s" change on each object. 数组中的所有对象都具有相同的结构,只是“ v”“ s”的值在每个对象上都发生变化。

I just need to extract the values of fields "v" and "s" from each object and save them in a CSV file with this exact format: 我只需要从每个对象中提取字段“ v”“ s”的值,然后将其保存为CSV文件即可,格式如下:

Date,Value
2019-05-14 12:00:00,3.2
2019-05-15 00:00:00,4.7
2019-05-17 05:00:00,1
2019-05-19 20:00:00,2.3
2019-05-28 08:00:00,33.4
2019-05-28 10:00:00,18.8
2019-05-28 12:00:00,11.5
2019-05-28 13:00:00,12.4
2019-05-29 06:00:00,6.4

Right now I'm doing some tests trying first to show the mentioned values from the array in console with a for loop (instead of just the console.log(datos) in the first code): 现在,我正在做一些测试,试图首先通过for循环显示控制台中数组中提到的值(而不是仅显示第一个代码中的console.log(datos)):

for (var i = 0; i < datos.length; i++) {
                console.log(datos[i]['data'['time'['s']]]);
            }

But I get the following output in the console: 但是我在控制台中得到以下输出:

undefined
undefined

Obviously my syntax is incorrect, but I don't know where is the problem. 显然我的语法不正确,但是我不知道问题出在哪里。
What would be the exact syntax to access the array and get the desired values? 访问数组并获取所需值的确切语法是什么?

Thank you very much to everyone that can throw some light into this. 非常感谢每个能对此有所启发的人。

You're close here. 你在这附近 Try this syntax. 试试这个语法。 Instead of nesting those items, separate them like such: 与其嵌套这些项目,不如将它们分开:

for (var i = 0; i < datos.length; i++) {
  console.log(datos[i]['data']['time']['s']);
}

You could also use dot notation to find what you're after: 您还可以使用点符号来查找所需内容:

for (var i = 0; i < datos.length; i++) {
  console.log(datos[i].data.time.s);
}

fiddle: https://jsfiddle.net/gn7kyhuz/ 小提琴: https : //jsfiddle.net/gn7kyhuz/

 var datos = [ { "data" : { "iaqi" : { "co" : { "v" : 3.2 } }, "time" : { "s" : "2019-05-14 12:00:00" } } }, { "data" : { "iaqi" : { "co" : { "v" : 4.7 } }, "time" : { "s" : "2019-05-15 00:00:00" } } } ]; for (var i = 0; i < datos.length; i++) { console.log(datos[i]['data']['time']['s']); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM