Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Query Returns Empty Array

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.

like image 851
Adam Weitzman Avatar asked Dec 17 '15 21:12

Adam Weitzman


People also ask

How do I check if a field is empty in MongoDB?

Use BsonNull. Value with the MongoDB C# driver to query for null or missing fields in MongoDB.

What is MongoDB elemMatch?

Definition. $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

How do you check if a value exists in an array in mongoose?

In your case you could try: Person. find({ members: { $elemMatch: { id: id1 } } });


1 Answers

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' });
like image 112
robertklep Avatar answered Oct 04 '22 15:10

robertklep