NodeJS和mongodb无法获得大量文档的结果

时间:2016-12-06 20:54:44

标签: node.js mongodb

当我查询大量文档时,我没有收到mongodb的任何回复。

这是回调问题吗?还是配置问题?我所要做的就是根据指定的查询计算文档数量。

我查询mongo的代码:

 /*jslint node:true */
'use strict';
var MongoClient = require('mongodb').MongoClient, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/cse541';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
    assert.equal(err, null);
    console.log("Connected correctly to server");
    var collection = db.collection("flights"), cursor = collection.find({year: 2015, month: 1, day: 1});

    collection.find({year: 2015, month: 1, day: 1}).count(function (err, number) {
        console.log(number);
        db.close();
    });


    //cursor.limit(1);
    /*
    cursor.each(function (err, doc) {
        if (err) {
            console.log(err);
        } else if (doc != null) {
            console.log('Fetched:', doc);
        } else {
            db.close();
        }
    });
    */

});   

使用计数时的输出:

PS C:\Users\Frank\Documents\CSE 541\Project\flightdelaytracker> node serverside

null

在代码中使用光标时的输出:

PS C:\Users\Frank\Documents\CSE 541\Project\flightdelaytracker> node serverside

{ MongoError: connection 0 to localhost:27017 timed out at Function.MongoError.create (C:\Users\Frank\Documents\CSE 541\Project\flightdelaytracker\node_modules\mongodb-core\lib\e
rror.js:29:11) at Socket.<anonymous> (C:\Users\Frank\Documents\CSE541\Project\flightdelaytracker\node_modules\mongodb-core\lib\connection\connection.js:186:20) at Socket.g (events.js:292:16) at emitNone (events.js:86:13) at Socket.emit (events.js:185:7) at Socket._onTimeout (net.js:342:8) at ontimeout (timers.js:365:14) at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5) name: 'MongoError', message: = 'connection 0 to localhost:27017 timed out' }

示例文件:

> db.flights_jan2015.find().limit(1).pretty();
{
        "_id" : ObjectId("5848971edcb453cbc93274f1"),
        "id" : 468982,
        "year" : 2015,
        "quarter" : 1,
        "month" : 1,
        "day" : 12,
        "day_of_week" : 1,
        "unique_carrier" : "UA",
        "airline_id" : "19977",
        "carrier" : "UA",
        "origin_airport" : "TPA",
        "origin_city" : "Tampa",
        "origin_state" : "FL",
        "arrival_airport" : "CLE",
        "arrival_city" : "Cleveland",
        "arrival_state" : "OH",
        "scheduled_dep_time" : 1125,
        "actual_dep_time" : 1121,
        "true_dep_delay" : -4,
        "dep_delay" : 0,
        "dep_delay_15_flag" : 0,
        "taxi_out_time" : 13,
        "taxi_in_time" : 5,
        "schedule_arr_time" : 1356,
        "actual_arr_time" : 1342,
        "true_arr_delay" : -14,
        "arr_delay" : 0,
        "arr_delay_15_flag" : 0,
        "cancelled_flag" : 0,
        "air_time" : 123,
        "distance" : 927,
        "carrier_delay" : 0,
        "weather_delay" : 0,
        "nas_delay" : 0,
        "security_delay" : 0,
        "late_aircraft_delay" : 0
}

1 个答案:

答案 0 :(得分:0)

试试这个并查看它是否返回计数:

/*jslint node:true */
'use strict';
var MongoClient = require('mongodb').MongoClient, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/cse541';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
    assert.equal(err, null);
    console.log("Connected correctly to server");
    var collection = db.collection("flights");

    collection.count({year: 2015, month: 1, day: 1}).then(function (number) {
        console.log(number);
        db.close();
    });
});  

告诉我发生了什么,我们将从那里继续。

编辑:让我们确保我们可以让您连接到mongodb

试试这个:

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/cse541';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
    assert.equal(err, null);
    console.log("Connected correctly to server");
    db.close();
});