Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Mongoose is connected but can't find data

I have a problem and could really use some help. We had an application that used MySQL and we are switching over to MongoDB. We are using Node.js with Express and have MongoDB and Mongoose for the database

We have looked at the MongoDB and Mongoose documentation and searched for related questions on Stack Overflow but we seem to be missing something.

This is what we have in app.js:

var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost:27017/DBName');

This is our usersModel.js

var db          = require('mongoose');
var Schema      = db.Schema;

var userSchema  = new Schema({
    u_id :                      Number,
    u_name :                    { type: String, required: true },
    u_lastname :                String
});

module.exports  = db.model('user', userSchema);

And this is what we use in the userController:

var User        = require('../models/usersModel');

User.find({}, function(err, users) {
    if (err) throw err;

    console.log("Found users: ", users);
});

The console.log(db.connection.readyState); says it is connecting. And the User.find() doesn't seem to give out an error, but instead gives an empty array/undefined.

We would really appreciate it if someone could tell us what we overlook.

Thanks in advance.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Not sure why anyone thought this was a good idea, but here's what mongoose does:

Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name.

As a bonus, it's in lower-case which sucks when you have table names with camel case convention.

You can fix it by setting below option in your scheme:

var userSchema = new Schema({..}, { collection: 'user' });

More on this here.

about 4 years ago · Santiago Trujillo Report

0

Your find() call looks fine to me.

Are you sure there are users in your collection? Does creating a new user throw an error that you didn't notice? Maybe you can verify that there are indeed users in your collection by popping open a terminal and writing:

> mongo
> use DBName
> show collections
> db.user.find()

And that will return all users that exist.

about 4 years ago · Santiago Trujillo Report

0

At: iuliu.net: We get both errors the first was [] and the second is undefined.

At JohnnyHK: Thank you for your submit, but we are sure the the user collection is in this database and the properties we search exists.

At Juuso: Thanks for your feedback your but in the mongo terminal we got the collection output.

Some one asked us a critical question if we tried this with monk. We installed monk and we have find the database, collection and got result back. We're still not sure what the problem with Mongoose was, but with monk it works.

Thank you guys for your feedback and time.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!