7

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.

5
  • Hmm, if you're getting a response back it's not likely to be a timeout I don't think (but it still could be, of course). Have you tried running a 'raw' mongo query on the command line? If you can try that and get the record back you want, then we can partition the problem and look more @ mongoose :) Commented Dec 17, 2015 at 21:59
  • and at the same time (but in a separate step) I would suggest running some sort of $has or $exists w/ mongoose to be sure you're getting results back at all :) Commented Dec 17, 2015 at 22:01
  • Was the database created with Mongoose as well (using the schema you post), or are you trying to retrofit a Mongoose schema on top of an existing database? Commented Dec 17, 2015 at 22:08
  • fitting a mongoose schema ontop of an existing database Commented Dec 17, 2015 at 22:09
  • thanks for reaching out mark, i haven't installed the shell yet....unless it came with the original mongodb package, i'll look into it. Commented Dec 17, 2015 at 22:26

1 Answer 1

29

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' });
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a million robert, that worked. So do i have this right...by default mongoose pluralizes what you name your model?
@AdamWeitzman the MongoDB collection name that Mongoose will use is the pluralized (and lowercased, I think) version of the model name (so if your model is called User, it will use the users collection).
You saved my day. I was thinking of some mistake in my code. It works fine now. I have updated your answer which works for me. Thank you man!!!!!
Wow. This gave me nightmares last night... 7:25am, and we're GTG - THX!
@robertklep Thanks mate! I really wonder why mongoose hasn't resolved this issue. Do we have any open ticket for this problem?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.