Confusion on MongoDB

I’m very confused with how to do “Create a Model” inside of the MongoDB & Mongoose Course

require('dotenv').config(); const mongoose = require('mongoose'); const { Schema } = mongoose; mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true}) .then(db=>{ console.log('MongoDB Atlas connected'); res.json = db; }) .catch(err=>console.log) const mongodb = require('mongodb'); /** # SCHEMAS and MODELS # /*  ====================== */ var Person /* = <Your Model> */ var createAndSavePerson = function(done) { done(null /*, data*/); }; var createManyPeople = function(arrayOfPeople, done) { done(null/*, data*/); }; var findPeopleByName = function(personName, done) { done(null/*, data*/); }; var findOneByFood = function(food, done) { done(null/*, data*/); }; var findPersonById = function(personId, done) { done(null/*, data*/); }; var findEditThenSave = function(personId, done) { var foodToAdd = 'hamburger'; done(null/*, data*/); }; var findAndUpdate = function(personName, done) { var ageToSet = 20; done(null/*, data*/); }; var removeById = function(personId, done) { done(null/*, data*/); }; var removeManyPeople = function(done) { var nameToRemove = "Mary"; done(null/*, data*/); }; var queryChain = function(done) { var foodToSearch = "burrito"; done(null/*, data*/); };

Challenge: Create a Model

Link to the challenge:

should be const {Schema}= require("mongoose")
or “require” the whole “default export” from mongoose as const mongoose = require("mongoose") then you can use the mongoose object properties such as mongoose.Schema, mongoose.connect , mongoose.model etc

@imehappen You can use destructuring just fine.

const mongoose = require("mongoose");
const { Schema } = mongoose;

Is the same as :

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

@nightmares Did you look at the docs as suggested in the challenge? Unlike the challenge, it has pretty clear examples of creating the schema and model right on the first page.

From the docs, I shortened the schema for simplification.

import mongoose from 'mongoose';
const { Schema } = mongoose;

const blogSchema = new Schema({
  title:  String, // String is shorthand for {type: String}
  author: String,
  body:   String

const Blog = mongoose.model('Blog', blogSchema);
1 Like

@lasjorg okey thank you have not considered/seen/used the other syntax before but it is good I have learned it. On the other hand:

A model is derived from a Schema and from the look of your code there is no Schema definition

A schema definition may look like:

const blogSchema = mongoose.Schema({
title:String, //or :point_down: this way
type: String,
},  //or :point_down: this way
type: ObjectId,
require:[true,"A post must have an author"],
default: some ObjectId value,

then from the Schema you can create a model as:

const Blog = mongoose,model("Blog", BlogSchema)

the Blog object (model) now can be used to create new blogs.

As a sidenote:

You will need to remove your MONGO_URI out of the sample env file because right now we all have access to it.

You don’t want to expose your keys for everyone to see.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.