0

I'm new to Javascript, Node.ja and MongoDB and I'm having trouble getting results back into my Node.js server when specifying query parameters (works fine returning all records with no query).

My collection has a timestamp field in the format "yyyy-mm-dd hh:mm:ss" and I want to return all results where the hour is either '00', '06', '12' or '18' to drive chart (using chart.js)

On the mongo cmdline I can do the following:

db.MyCollection.find({ $or: [ {"timestamp": /.* 06:./}, {"timestamp": /. 12:./}, {"timestamp": /. 18:./}, {"timestamp": /. 00:.*/} ]})

This returns what I expect. However, I can't get this to work in my Node.js server.

function getChartData_7d(callback) {
    MongoClient.connect(mongoURL, function(error, client) {
        if (error) {
            throw err;
        }

        var db = client.db('WeatherDB');
        var collection = db.collection('AverageTPH');

        var query = {
            $or:[
                {timestamp: /.* 06:.*/}, 
                {timestamp: /.* 12:.*/}, 
                {timestamp: /.* 18:.*/}, 
                {timestamp: /.* 00:.*/}
            ]
        };

        /*
        ** Even this query doesn't return anything
        */
        var query = {
            timestamp: /.* 06:.*/ 
        };

        /*
        ** But this does...
        */
        var query = {};

        var options = {
            "limit": 7
        };

        /*
        ** Get a reading 4 times a day at 06:xx, 12:xx, 18:xx, 00:xx for 7 days...
        */
        collection.find(query, options).sort({timestamp: -1}).toArray((error, items) => {
            if (error) {
                throw error;
            }

            return callback(items);
        });

        client.close();
    });
}

I've looked at examples and read the MongoDB manual but I'm scratching my head trying to get this to work. Any help would be much appreciated.

  • can you please post a sample document and required output? – JBone Jun 30 '19 at 17:24
  • Querying date parts using regex is not a good idea. This will be very slow with all the `$or`s unanchored regex, and this early in the development you're already hitting issues with short example codes. Consider using a proper DateTime datatype and using MongoDB's date aggregation facilities: https://stackoverflow.com/questions/34914737/mongodb-aggregate-convert-date-to-another-timezone/48535842#48535842 – kevinadi Jul 01 '19 at 01:15
  • Thank you @kevinadi, I'll take a look at that. Still wondering why the query isn't working though, as I'm clearly missing something. – user2754142 Jul 01 '19 at 08:30
  • OK, to answer @JBone. Sample record (I guess this is what MongoDB calls a document) is: { "_id" : ObjectId("5d1997673227390b14e18d05"), "timestamp" : "2019-07-01 06:17:27", "temperature" : "22.52", "pressure" : "1017.90", "humidity" : "53.81" }. This is for a weather station, in case you were wondering! Required output from the query would be all records where the hour is either 06, 12, 18 or 00. – user2754142 Jul 01 '19 at 08:31
  • OK, I've found the solution. As there is little useful documentation and examples that I could find, I've dumped MongoDB and installed Postgres instead, works like a charm! – user2754142 Jul 06 '19 at 13:37

1 Answers1

-1

OK, I've found the solution. As there is little useful documentation and examples that I could find, I've dumped MongoDB and installed Postgres instead, works like a charm!