Mongoose not Creating Collection

I’m trying to utilize a MongoDB Database from my Node app. However, the collection isn’t being created automatically.

mongoose.connect("mongodb+srv://username:password@cluster0.fnl7f.mongodb.net/admin/MRactivityDB", { useUnifiedTopology: true })

Schema File:

const mongoose = require('mongoose')

const MRactivity = mongoose.Schema({
  username: String,
  userid: String,
  activity: String,
})

module.exports = mongoose.model("MRactivity", MRactivity)

(Username and password in the collection string are the actual credentials. I just removed them for security reasons in this post.)

If anyone with some more experience using Mongoose could tell me what the issue is, help would be appreciated!

That is not how collections work. If you know MySQL, it would make more since. Basically the database is the database the collections are tables. Schemas are used to insert data.

Collections don’t use Schemas. You can write anything, in any order.

If I remember correctly, once you save something, it will create it. Your code is fine and you do need to use a schema, contrary to what @chessebuilderman has said.

He is trying to make a collection with a schema.

@SuperWaltC That’s what you are supposed to do when using Mongoose
From the docs:

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

Please read the docs if you don’t understand how it works: https://mongoosejs.com/docs/guide.html

Thanks for replying. I tried to save something earlier and nothing appeared in the compass.


const MRactivity = require("./MRactivity.js")

const newActiv = new MRactivity({
        username: "azlentic",
        userid: "12345",
        activity: "5",
    });
newActiv.save()

SQL is unrelated to Mongodb, you are trying to put down a person for making a legitimate question. They only forgot to save the data to mongodb

  1. Don’t bump posts.
  2. Where did SQL come from? Finish this conversation in DMs.

How can I DM you? No idea