How to get Data from MongoDb using mongoose?

2020-03-12 03:15发布

I just started learning MongoDB and mongoose. Currently I have the following structure:

database   -> skeletonDatabase
collection -> adminLogin

When I run db.adminLogin.find() from the command line I get:

{ "_id" : ObjectId("52lhafkjasfadsfea"), "username" : "xxxx", "password" : "xxxx" }

My connection (this works, just adding it FYI)

module.exports = function(mongoose)
{
    mongoose.connect('mongodb://localhost/skeletonDatabase');

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
        console.log('Conntected To Mongo Database');
    });
}

My -js-

module.exports = function(mongoose)
{
    var Schema = mongoose.Schema;

    // login schema
    var adminLogin = new Schema({
        username: String,
        password: String
    });

    var adminLoginModel = mongoose.model('adminLogin', adminLogin);
    var adminLogin = mongoose.model("adminLogin");

    adminLogin.find({}, function(err, data){
        console.log(">>>> " + data );
    });
}

My console.log() returns as >>>>

So what am I doing wrong here? Why do I not get any data in my console log? Thanks in advance for any help.

3条回答
够拽才男人
2楼-- · 2020-03-12 03:47

first compile just one model with the schema as an argument

var adminLogin = mongoose.model('adminLogin', adminLogin);

in your code adminLogin does not exist, adminLoginModel does;

after that ,instead to

adminLogin.find({}, function(err, data){
        console.log(">>>> " + data );
    });

try this

adminLogin.find(function (err, adminLogins) {
  if (err) return console.error(err);
  console.log(adminLogins);

is important the "s" because mongo use the plural of the model to name the collection, sorry for my english...

查看更多
地球回转人心会变
3楼-- · 2020-03-12 03:51

mongoose by default takes singular model names and pairs them with a collection named with the plural of that, so mongoose is looking in the db for a collection called "adminLogins" which doesn't exist. You can specify your collection name as the 2nd argument when defining your schema:

var adminLogin = new Schema({
    username: String,
    password: String
}, {collection: 'adminLogin'});
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-03-12 03:51

Had a problem with injecting it within an express route for my api so I changed it thanks to @elkhrz by first defining the schema and then compiling that one model I want to then pull like so:

app.get('/lists/stored-api', (req, res) => {

    Apis.find(function(err, apis) {

        if (err) return console.error(err);

        res.send(apis);

    });

});

I wouldn't send it to the body, I would actually do something else with it especially if you plan on making your API a production based application.

Run through this problem and read up on possible proper ways of rendering your data: How to Pass Data Between Routes in Express

Always a good idea to practice safe procedures when handling data.

查看更多
登录 后发表回答