skip to Main Content

I have set up a database in MongoDB called entertainment with a collection called medias. This collection has JSON data within it.

When I make a call to the DB using Postman: http://localhost:8000/data I get an empty array returned.

Here is my entire code:

const express = require("express");
const mongoose = require("mongoose");
const app = express();
require("dotenv").config();

const uri = `mongodb+srv://tyrellc:${process.env.MONGO_PASSWORD}@entertainmentapp.jtcf92n.mongodb.net/?retryWrites=true&w=majority`;

async function connect() {
  try {
    await mongoose.connect(uri);
    console.log("Connected to MongoDB");
  } catch (error) {
    console.log(error);
  }
}

mongoose.set("strictQuery", false);

connect();

app.listen(8000, () => {
  console.log("Express Server Started port:8000");
});

const collectionSchema = new mongoose.Schema({
  _id: { type: mongoose.Schema.Types.ObjectId, required: true },
  title: { type: String, required: true },
  thumbnail: {
    trending: {
      small: { type: String, required: true },
      large: { type: String, required: true },
    },
    regular: {
      small: { type: String, required: true },
      medium: { type: String, required: true },
      large: { type: String, required: true },
    },
  },
  year: { type: Number, required: true },
  category: { type: String, required: true },
  rating: { type: String, required: true },
  isBookmarked: { type: Boolean, default: false },
  isTrending: { type: Boolean, default: true },
});

const Collection = mongoose.model("Collection", collectionSchema, "medias");

app.get("/data", async (req, res) => {
  const data = await Collection.find();
  res.json(data);
});

When running the app, I get both console logs: Express Server Started port:8000 and Connected to MongoDB .

This is what my JSON data looks like in MongoDB:

{"_id":{"$oid":"63c733940231fb2f67b5344e"},"title":"Beyond Earth","thumbnail":{"trending":{"small":"./assets/thumbnails/beyond-earth/trending/small.jpg","large":"./assets/thumbnails/beyond-earth/trending/large.jpg"},"regular":{"small":"./assets/thumbnails/beyond-earth/regular/small.jpg","medium":"./assets/thumbnails/beyond-earth/regular/medium.jpg","large":"./assets/thumbnails/beyond-earth/regular/large.jpg"}},"year":{"$numberInt":"2019"},"category":"Movie","rating":"PG","isBookmarked":false,"isTrending":true}

I have looked into pluralization and have tried:

const Collection = mongoose.model("Collection", collectionSchema, "medias");

const Collection = mongoose.model("Collection", collectionSchema, "media");

const Collection = mongoose.model("media", collectionSchema);

const Collection = mongoose.model("medias", collectionSchema);

Nothing seems to do the trick. Does anyone have any idea what I am doing wrong?

MongoDB Atlas Database:
MongoDB Atlas Database

2

Answers


  1. According to mongoose documentation, find() method accepts parameters: filter type object required, and optional parameters.

    If you want to retrieve all documents from the model you should pass an empty object {} as a filter.

    app.get("/data", async (req, res) => {
      const data = await Collection.find({});
      res.json(data);
    });
    
    Login or Signup to reply.
  2. You must specify which database to use. You can either do this in your connection string, like this:

    const uri = `mongodb+srv://tyrellc:${process.env.MONGO_PASSWORD}@entertainmentapp.jtcf92n.mongodb.net/entertainment?retryWrites=true&w=majority`;
    

    Or if you prefer, you can pass it as an option to mongoose.connect(), like this:

    await mongoose.connect(uri, { dbName: 'entertainment' });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search