Josee
Josee

Reputation: 180

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.

Upvotes: 1

Views: 3544

Answers (4)

dave 2 smith
dave 2 smith

Reputation: 1

You have to specify which database you want to interact with. In this case "sample_mflix".

So you can update your config file to look more like this

const conn = await mongoose.connect(process.env.MONGO_URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        dbName: 'sample_mflix'
    });

Upvotes: 0

benmazouz seif eddine
benmazouz seif eddine

Reputation: 59

findById is an async function so you need to use await/async :

router.get('/questions',async (req, res) => {
   try{
    const data = await movies.findById('573a1390f29313caabcd4135')
    console.log(data)
    }catch(e){
     console.log(e) ;
    }


})




or you can use promise :

router.get('/questions', (req, res) => {
      movies.findById('573a1390f29313caabcd4135')
             .then(data=>console.log(data)) 
             .catch(err=>console.log(err)

})




Upvotes: 1

Jo&#227;o Bordalo
Jo&#227;o Bordalo

Reputation: 46

Try this

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

})

Or

router.get('/questions', (req, res) => {
    movies.findById('573a1390f29313caabcd4135')
    .then(data => {
         console.log(data)
     });
})

Upvotes: 1

Jo&#227;o Bordalo
Jo&#227;o Bordalo

Reputation: 46

Did you also swap out the password and username?

Upvotes: 1

Related Questions