簡體   English   中英

用MongoDB和Node.js查詢不返回任何結果

[英]Query with MongoDB and Node.js not returning any results

我是Java,Node.ja和MongoDB的新手,並且在指定查詢參數時無法將結果返回到Node.js服務器中(可以很好地返回所有記錄而無需查詢)。

我的收藏集的時間戳字段格式為“ yyyy-mm-dd hh:mm:ss”,我想返回所有小時為“ 00”,“ 06”,“ 12”或“ 18”的結果圖表(使用chart.js)

在mongo cmdline上,我可以執行以下操作:

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

這返回了我的期望。 但是,我無法在Node.js服務器中使用它。

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();
    });
}

我已經看過示例並閱讀了MongoDB手冊,但是我想盡一切辦法使它起作用。 任何幫助將非常感激。

好的,我找到了解決方案。 由於我幾乎找不到有用的文檔和示例,因此我轉儲了MongoDB並安裝了Postgres,它的工作原理很吸引人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM