Giter VIP home page Giter VIP logo

mongo-iii-mini-solution's Introduction

Mongo III Mini Sprint

Topics

Running the Project

  • cd into your project directory
  • npm install to install dependencies
  • Run mongod to start your Mongo server from your root directory or create a data directory in this project

Mongoose's Populate Method

For this mini sprint, there are two models we'll be utilizing: the Post model and the Comment model. The Comment model depends upon the Post model. You'll notice that the in the Post model schema we have this line

comments: [{ type: mongoose.Schema.Types.Number, ref: 'Comment' }]

This line specifies the comments field on the Post schema, which is an array populated by comment IDs. This is where the populate() method that Mongoose provides comes in. When we fetch a post, we want to also see all of its associated comments on the posts that we get back from the database. Without running populate(), our posts would come back with an array of just IDs of all of their comment children, without the comments' actual data. Here's an example of running the populate() command

Post.findById(3).exec()
   .then((post) => {
   	post.populate('comments');
   	res.send(post);
   }

Model / Controller / Routes Organization

Take a look at how the files in this sprint are organized. The model directory holds the schemas. The routes directory specifies all of the routes of our little API. The controller directory holds all of the logic that needs to be executed upon querying individual endpoints.

Think of this style of organization as analogous to the Model / View / Controller paradigm that is still commonly used in front end applications. It's just a way of organizing and modularizing different parts of what our project needs to do in a logical fashion. Organizing your code in this way makes it easier to find files in the future when a project scales and becomes much larger in terms of sheer number of lines of code. Also, a developer without any prior knowledge of the project can look around and orient themselves much more easily with an organization pattern such as this.

Todos

  • Implement all of the different controller methods in the controllers/postControllers.js file.
  • Test your implementation locally on your Mongo server with either Postman, or via your terminal.

mongo-iii-mini-solution's People

Contributors

seanchen1991 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.