Ayeye Brazo
Ayeye Brazo

Reputation: 3476

How to get data from existing MongoDB database?

I have a database on mlab and now I was starting a new Project and trying to simply get data from there.

The Database has only one collection called Article.

On my Node js project, using Mongoose, I created the Model for it:

var mongoose = require('mongoose');

var articleSchema = new mongoose.Schema({
  title: { type: String, required: true },
  body: { type: String }
});

var Article = mongoose.model('Article', articleSchema);

module.exports = Article;

The in my controller I just did this:

Article.find({}, function (err, articles) {
    res.send(articles);
});

I should receive more than 300 articles but the response is just an empty Array.

I was wondering if I need to run a few more command in order to connect to the db correctly, but I don't know it...

Upvotes: 3

Views: 2246

Answers (1)

Thomi
Thomi

Reputation: 649

If you want to fetch on an existing Article collection:

var articleSchema = new mongoose.Schema({
  title: { type: String, required: true },
  body: { type: String }
}, { collection : 'Article' });

Upvotes: 5

Related Questions