I'm trying to query the data in my MongoDB database from my app using the Mongoose module, and what's being returned is an empty array.
The most common answer in SO is that Mongoose pluralizes the collection name when creating the model. However, I believe I have no problem there (if actually I'm doing this wrong, please explain it to me).
At this point, all I've tried doing is selecting all of the entries using Result.find({}, callback), but again, just an empty array is returned.
When I query the data on the command line, I get the expected result. Below I just retrieved one entry for brevity.
Mongo shell on command line:
> db.getName()
kaplanFfl
> db.getCollection('results')
kaplanFfl.results
> db.results.findOne({ Owner: 'Sample'})
{
"_id" : ObjectId("5a24c90b46521500830aa042"),
"Owner" : "Sample",
"Pts" : 81.5,
"Week" : 1,
"SeasonAvg" : 86,
"Lst8_Avg" : 92,
"Lst4_Avg" : 91,
"Mean" : 82,
"High" : 116,
"Low" : 55,
"Diff" : 61,
"Dev" : 16.49141822,
"Elite" : 0,
"Superior" : 0,
"Inferior" : 0,
"Abyssmal" : 0,
"MeanPlus" : 0,
"MeanMinus" : -1
}
Here's the code:
model.js
'use strict';
const mongoose = require('mongoose');
let Schema = mongoose.Schema;
let resultSchema = new Schema({
Owner: String,
Pts: Number,
Week: Number,
SeasonAvg: Number,
Lst8_Avg: Number,
Lst4_Avg: Number,
Mean: Number,
High: Number,
Low: Number,
Diff: Number,
Dev: Number,
Elite: Number,
Superior: Number,
Inferior:Number,
Abyssmal: Number,
MeanPlus: Number,
MeanMinus: Number
})
let WeeklyResult = mongoose.model('result', resultSchema);
module.exports = WeeklyResult;
index.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const Result = require('./models/models');
const PORT = process.env.PORT || 3000;
//username and password has been removed, but it does connect
const uri = 'mongodb://<username>:<password>@kaplanffl-shard-00-00-rmbku.mongodb.net:27017,kaplanffl-shard-00-01-rmbku.mongodb.net:27017,kaplanffl-shard-00-02-rmbku.mongodb.net:27017/kaplanFfl?ssl=true&replicaSet=kaplanFfl-shard-0&authSource=admin'
//open the connection to the database
mongoose.Promise = global.Promise;
mongoose.connect(uri, {
useMongoClient: true
})
let db = mongoose.connection;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
db.on('error', console.error.bind(console, 'Connection error: '));
db.once('open', function() {
console.log('Connection to MongoDB is live. Database: ' + db.name);
Result.find({}, function(err, results) {
if (err) console.log(err);
console.log(results);
})
})
//start web server
app.listen(PORT, (req, res) => {
console.log('Server live on port ' + PORT);
});
I expect to get all of the results, but only get the following in the console:
Server live on port 3000
Connection to MongoDB is live. Database: kaplanFfl
[]
So, I feel as though the pluralization problem is covered off on, right? I've tried the numerous ways that are on SO and still get the same result.
Any help is appreciated!