Fetching Sample Data from MongoDB Atlas with mongoose
NickName:Josee Ask DateTime:2020-01-22T08:54:05

Fetching Sample Data from MongoDB Atlas with mongoose

As the title suggests, I am trying to fetch existing data that already exists inside MongoDB Atlas. The data comes from the mongoDB Atlas sample data, and I am using the sample_mflix database. I have done some research and found out that I have to copy the model first and name the collection name.

For the following, I tried fetching the movie collection data:

//Movie model
const mongoose = require('../db/mongoose')
//const mongoose = require('mongoose)

const moveiesSchema = new mongoose.Schema({
    plot:{
        type:String
    } ,
    genres:[{
        type:String
    }],
    runtime:{
        type:Number
    },
    cast:[{
        type: String
    }],
    num_mflix_comments:{
        type: Number
    },
    title:{
        type: String
    },
    countries:[{
        type: String
    }],
    released:{
        type: Date
    },
    directors:[{
        type: String
    }],
    rated:{
        type: String
    },
    awards:{
        wins:{
            type: Number
        },
        nominations: {
            type: Number
        },
        text: {
            type: String
        }
    },
    lastupdated:{
        type: String
    },
    year: {
        type: Number
    },
    imdb:{
        rating: {
            type: Number
        },
        votes:{
            type: Number
        },
        id:{
            type: Number
        }
    },
    type: {
        type: String
    },
    tomatoes:{
        viewer:{
            rating: {
                type: Number
            },
            numReviews:{
                type: Number
            },
            meter:{
                type: Number
            }
        },
        lastupdated: {
            type:Date
        }
    }


}, {collection: 'movies'});

const movies = mongoose.model('movies', moveiesSchema)

module.exports = movies

Here is my router:

//sample_mflix-router.js 
const express = require('express')
const router = new express.Router()

const movies = require('../models/movies_model')



//Currently not working
router.get('/questions', (req, res) => {
    const data = movies.findById('573a1390f29313caabcd4135')
    console.log(data)

})


module.exports = router 

Idk what the problem is as I just followed this guy: Getting NULL data from collection from MongoDB Atlas

and this guy: How to get data from existing MongoDB database?

I was thinking that perhaps I did the model wrong, or perhaps my mongo URL is wrong as well. Here is my URL connection code

MONGODB_URI=mongodb+srv://<user>:<password>@cluster0-rebv7.mongodb.net/sample_mflix?retryWrites=true&w=majority

I swapped out test with sample_mflix because I read on another post that I should use the database name instead of the default name given 'test'.

When I try to make the get request, all i get is null. Idk what the problem is, so I hope one of you guys can help.

edit: Here is my connection file:

mongoose.connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
})

module.exports = mongoose

I am pretty sure this connects to MongoDB Atlas as it says so on the cluster page.

Copyright Notice:Content Author:「Josee」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59851246/fetching-sample-data-from-mongodb-atlas-with-mongoose

Answers
João Bordalo 2020-01-22T01:13:15

Try this\n\nrouter.get('/questions', async (req, res) => {\n const data = await movies.findById('573a1390f29313caabcd4135')\n console.log(data)\n\n})\n\n\nOr\n\nrouter.get('/questions', (req, res) => {\n movies.findById('573a1390f29313caabcd4135')\n .then(data => {\n console.log(data)\n });\n})\n",


João Bordalo 2020-01-22T00:57:47

Did you also swap out the password and username? ",


More about “Fetching Sample Data from MongoDB Atlas with mongoose” related questions

Fetching Sample Data from MongoDB Atlas with mongoose

As the title suggests, I am trying to fetch existing data that already exists inside MongoDB Atlas. The data comes from the mongoDB Atlas sample data, and I am using the sample_mflix database. I h...

Show Detail

Unable to receive results from MongoDB Atlas using Mongoose

I'm having a hard time figuring out why my code isn't letting me get the documents that are hosted in my MongoDB Atlas cluster. I've made a small example that has the same problem I'm experiencing.

Show Detail

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 hav...

Show Detail

Mongoose is returning empty array while fetching data from MongoDb Atlas. Why?

App.js file-&gt;Connection is established successfully, but in find() callback,data is empty[] const express = require('express'); const mongoose = require('mongoose'); const Users = re...

Show Detail

Mongoose not retrieving data from atlas

Can someone help me with this problem, mongoose not fetching any data from MongoDB atlas? Everything is fine and I am able to fetch data from cmd, but with mongoose, it returns an empty array. ...

Show Detail

Unable to fetch data from mongodb- atlas?

I am trying to fetch data from MongoDB-atlas using note.js which I have manually fed to the server. The status of the collection on MongoDB-atlas sever is : dbname: viit collection name : viit_at...

Show Detail

not accessing old data on mongodb atlas by Node application

I have not used mongodb atlas for 10 days. when I am trying to connect node application after 10 days into mongodb atlas returning blank array in find Query from Node application , when I inserted...

Show Detail

How to connect to mongoDB Atlas using mongoose

I'm trying to connect to my cluster on mongoDB Atlas via Mongoose.connect(), but every time i try to connect i get an exception "MongoError: authentication fail" I know MongoDB Atlas is new mongo...

Show Detail

Node.js and MongoDB Atlas Mongoose Connection Error

I am trying to connect a MongoDB atlas instance to a nodejs/express server : const mongoose = require("mongoose"); const dbURI = "mongodb+srv://(url copied from atlas connect)"; const options =...

Show Detail

Mongoose failed to connect and Atlas MongoDB expect to whitelist IP while all IP has allowed

I hope someone could help me because I confused with this problem I thought my code is OK but Atlas seems reject so I couldn't connect to Atlas. The connection so simple. I fill those params from n...

Show Detail