【问题标题】:How to get the name of the atributes in mongodb using nodejs [duplicate]如何使用节点js获取mongodb中属性的名称[重复]
【发布时间】:2017-11-15 19:47:37
【问题描述】:

我有这些数据:

{ moteid: 'AA:11',
  temperature: '32',
  humidity: '17.0',
  readingDate: 2017-03-12T14:12:00.000Z,
  _id: 5940221b2c4e2c63974609af }

我通过以下操作得到:

result.forEach(function(entries) {
  console.log(entries);
})

如何在运行时将字段名称(即:moteid、温度、湿度、readDate)获取到数组中?

我想要字段名称,因此在接下来的步骤中我可以将它们用作变量。 我不想要值,只想要键。

我想要唯一的值,所以如果我有 100 条记录,我只想显示一次密钥。

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    使用Object.keys()

    var result =  { 
     moteid: 'AA:11',
     temperature: '32',
     humidity: '17.0',
     readingDate: new Date(2017-03-12T14:12:00.000Z),
     _id: "5940221b2c4e2c63974609af"
     }
    
    var attributes = Object.keys(result);
    
    console.log(attributes)
    // [ 'moteid', 'temperature', 'humidity', 'readingDate', '_id' ]
    

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 2012-02-29
      • 2010-10-29
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 2013-03-15
      相关资源
      最近更新 更多