Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda