admin管理员组

文章数量:1303452

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.

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.

Share Improve this question asked Apr 20, 2017 at 16:08 Mark_at_SwingByMark_at_SwingBy 411 silver badge4 bronze badges 2
  • Is it an empty array or is it undefined? – iuliu Commented Apr 20, 2017 at 16:17
  • Are your docs in the users collection? Possible dupe of stackoverflow./questions/14183611/… – JohnnyHK Commented Apr 20, 2017 at 18:18
Add a ment  | 

3 Answers 3

Reset to default 10

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.

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.

At: iuliu: 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.

本文标签: javascriptMongoose is connected but can39t find dataStack Overflow