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 >> >, ); 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://:@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