MongoDB Query Returns Empty Array

谁说胖子不能爱 提交于 2020-12-29 17:51:38

问题


Have a basic express app going that is connected to an almost .5 GB MongoDB Database...When I run:

router.get('/', function(req, res, next) {
    medical_data.find({'State':'CT'}, function(err, data) {
    console.log(data)
    res.render('index');
    });
});

I get a blank array returned:

[]
GET / 304 87.233 ms - -
GET /stylesheets/style.css 304 4.842 ms - -

Here is the entry from MongoLab that I'm trying to query for:

{
    "_id": {
        "$oid": "5671dfafd7f6fdd02436682e"
    },
    "Street": "65 KANE ST",
    "City": "WEST HARTFORD",
    "State": "CT"
}

And here is my medical_data model:

var mongoose = require('mongoose');

var medical_data_schema = new mongoose.Schema({
  Street: String,
  City: String,
  State: String
});

var medical_data = mongoose.model('medical_data', medical_data_schema);
// Make this available to our other files
module.exports = medical_data;

Why am I getting a blank array back? If I run findOne instead of find I get null in the console

I've run other succesfull node apps before but none with a database as big as this, so I think it might be a timeout issue? I'm not sure, any help would be amazing.


回答1:


Fitting a Mongoose schema on top of an existing database can be tricky. For one, Mongoose will determine the collection name by pluralizing the model name; so in your case, Mongoose will use the collection medical_datas, and my guess is that it's actually called medical_data.

You can specify the collection name to use for a schema by using the collection option:

var medical_data_schema = new mongoose.Schema({
  Street : String,
  City   : String,
  State  : String
}, { collection : 'medical_data' });


来源:https://stackoverflow.com/questions/34344492/mongodb-query-returns-empty-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!