Wednesday, 7 June 2017

Collection Distinct Field

I have the following a collection wich is return when I do the following commands:

var collection = db.collection('readings');
  collection.find({}).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);

Result:

[ { Node: '1',
    Type: 'temperature',
    Value: '23.2',
    _id: 5936f90c81c543363d42f593 },
  { Node: '2',
    Type: 'temperature',
    Value: '25.1',
    _id: 5936f90c81c543363d42f594 },
  { Node: '1',
    Type: 'temperature',
    Value: '26.5',
    _id: 5936f90c81c543363d42f595 },
  { Node: '3',
    Type: 'temperature',
    Value: '27.4',
    _id: 5936f90c81c543363d42f596 },
  { Node: '1',
    Type: 'temperature',
    Value: '-2.7',
    _id: 5936f90c81c543363d42f597 },
  { Node: '2',
    Type: 'temperature',
    Value: '25.9',
    _id: 5936f90c81c543363d42f598 },
  { Node: '3',
    Type: 'temperature',
    Value: '31.0',
    _id: 5936f90c81c543363d42f599 },
  { Node: '1',
    Type: 'humidity',
    Value: '45.2',
    _id: 5936f90c81c543363d42f59a },
  { Node: '2',
    Type: 'humidity',
    Value: '55.3',
    _id: 5936f90c81c543363d42f59b },
  { Node: '1',
    Type: 'humidity',
    Value: '59.6',
    _id: 5936f90c81c543363d42f59c },
  { Node: '3',
    Type: 'humidity',
    Value: '61.8',
    _id: 5936f90c81c543363d42f59d },
  { Node: '1',
    Type: 'humidity',
    Value: '17.0',
    _id: 5936f90c81c543363d42f59e },
  { Node: '2',
    Type: 'humidity',
    Value: '55.0',
    _id: 5936f90c81c543363d42f59f },
  { Node: '3',
    Type: 'humidity',
    Value: '81.0',
    _id: 5936f90c81c543363d42f5a0 } ]

How can I load to an array every unique node?

When I do:

var nodes = result.distinct('Node');

Inside this loop:

result.forEach(function(values){

I get the following error:

/home/user/node_modules/mongodb/lib/utils.js:123
    process.nextTick(function() { throw err; });
                                  ^

TypeError: result.distinct is not a function

What am I doing wrong here? I've read the documentation and I just can't figure it out... Any help is greatly appreciated. Best regards.



via joarafa

No comments:

Post a Comment