[Fixed] Mongoose with MongoDB Atlas return empty array


I’m trying to connect express.js with MongoDB Atlas and mongoose but server always returns an empty array ‘[ ]’. When I load a local database everything works (MongoDB locally and MongoDB Atlas have the same values)


const mongoose = require('mongoose');
const HistoryData = require('../models/databaseHistory');

exports.data = (req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    mongoose.connect('mongodb+srv://olechbartlomiej:<myPass>@quizapp-mpygt.mongodb.net/test?retryWrites=true', {useNewUrlParser: true});
    mongoose.Promise = global.Promise;
    HistoryData.find({ type : 'question' }).then((historyData) => {


const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const dataSchema = new Schema({
    type: {
        type: String
    data: {
        type: String

const HistoryData = mongoose.model('history', dataSchema);
module.exports = HistoryData;

MongoDB Atlas collection:
(Database name : test, collection name : history)

    _id : 5c9bdb721c9d440000345d62
    type : "question"
    data : "test test"

screen: MongoDB Atlas screen

And the server returns [ ]


You need to specify the database name as an option when connecting.
Update the options object to this:
{useNewUrlParser: true, dbName: "YOUR-DB-NAME-HERE"}

Leave a Reply

(*) Required, Your email will not be published