Giter Site home page Giter Site logo

intro-mongo-db's Introduction

Intro to MongoDB

Scott Moss & Frontend Masters

Resources

Course

Thanks for taking the Introduction to MongoDB course, created by Scott Moss & Frontend Masters. This course aims to cover a wide intro into using MongoDb with Nodejs. Topics refrain from going deep, but instead, focus on a wide view.

Exercises

Installing MongoDB

There are many ways to install MongoDB. The offical website has you covered either way. After installation, you might have to add a dbPath, a location where mongodb saves your data. You can do so like this.

mkdir -p /data/db

Note: If you have an error like "data directory not found" or "permission denied" while installing MongoDB:

sudo mkdir -p /data/db
sudo chown -R $USER /data/db

Models

  • location - exercises/models
  • commands
    • test - yarn test exercises/models/__test__/ or npm test exercises/models/__test__/

This exercise will have you create connection logic and mongoose schemas. Using the schema, you must create some CRUD functionality.

  • check out to the start branch
  • install node modules with npm or yarn
  • check the README on how to run test
  • create connection logic on connect.js
  • finish the user schema so that the the user model tests pass
  • complete the crud functions with the user model and get all the crud test to pass

Queries

  • location - exercises/queries
  • commands
    • test - yarn test exercises/queries/__test__/ or npm test exercises/queries/__test__/

This exercise will have you add relationships between models. You'll then use those models to create slightly more advanced queries than the last exercise

  • check out to the start branch
  • check the README on how to run your test
  • the post model should have have a one-to-one author field that points to the author collection
  • the post model should have a one-to-many similarPost field that points to posts
  • get all the post model tests to pass
  • get all the query tests to pass

Hooks

  • location - exercises/hooks
  • commands
    • test - yarn test exercises/hooks/__test__/ or npm test exercises/hooks/__test__/

In this exercise, you'll learn how to use schema middleware and virtuals. Also, you'll dig into indexes in more detail and create compound indexes.

  • check out to the start branch
  • check the README on how to run your test
  • add a compound index to the project schema so that project names are unique per org
  • add a virtual getter to the project schema called budgetLeft that calculates how much budget is left vs how much is spent so far
  • add a post remove hook to the org schema that removes all projects associated with the org
  • add a virtual getter to the org schema called avatar that creates the fill url to the org avatar by concatinating the cdnUrl with the org id
  • get all org tests to pass
  • get all project tests to pass

App

  • location - exercises/app
  • commands
    • start the server - node exercises/app/index.js

In this exercise, you'll have to create queries in Expressjs controllers to satisfy the request. You'll learn how to use mongodb in an API environment. You'll also have the couse to use a hosted MongoDB.

  • check out to the start branch
  • check the README on how to run your server
  • create db query for GET /todo/:id
  • create db query for GET /todo
  • create db mutation for POST /todo/
  • optional create a mLab sandbox and use your hosted DB

Debugging

Note: To handle the error MongoError: E11000 duplicate key error collection drop the database.

mongo
use dbName;
db.dropDatabase();
exit

(dbName is the name of the database)

intro-mongo-db's People

Contributors

1marc avatar corderophi678 avatar hendrixer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

intro-mongo-db's Issues

MongoError: server is closed

Getting this error MongoError: server is closed right before the should have correct fields user test when running this command: yarn test exercises/models/__test__/user.spec.js. Strangely if I remove the previous betaUser should default to false test, the error goes away.

Full error:


    MongoError: server is closed

      at executeWriteOperation (node_modules/mongodb/lib/core/sdam/server.js:439:14)
      at Server.insert (node_modules/mongodb/lib/core/sdam/server.js:372:5)
      at Object.callback (node_modules/mongodb/lib/core/sdam/topology.js:938:15)
      at processWaitQueue (node_modules/mongodb/lib/core/sdam/topology.js:1036:21)
      at NativeTopology.selectServer (node_modules/mongodb/lib/core/sdam/topology.js:440:5)
      at executeWriteOperation (node_modules/mongodb/lib/core/sdam/topology.js:906:12)
      at NativeTopology.insert (node_modules/mongodb/lib/core/sdam/topology.js:595:5)
      at NativeTopology.insert (node_modules/mongodb/lib/topologies/native_topology.js:54:11)
      at insertDocuments (node_modules/mongodb/lib/operations/common_functions.js:259:19)
      at InsertOneOperation.execute (node_modules/mongodb/lib/operations/insert_one.js:26:5)
      at executeOperation (node_modules/mongodb/lib/operations/execute_operation.js:77:17)
      at Collection.Object.<anonymous>.Collection.insertOne (node_modules/mongodb/lib/collection.js:516:10)
      at NativeCollection.<computed> [as insertOne] (node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:171:28)
      at model.Object.<anonymous>.Model.$__handleSave (node_modules/mongoose/lib/model.js:273:33)
      at model.Object.<anonymous>.Model.$__save (node_modules/mongoose/lib/model.js:337:8)
      at node_modules/kareem/index.js:278:20```

Drop database after test.

There is many databases creating from test since I use VS-Code test runner but it not getting removed after test.

Jest failing the test: Thrown:Undefined

Whenever I try to test the first exercise. Jest throws an error Thrown: Undefined for all tests. Searching online it seems to a problem with the async code, I tried to fix it myself but still it errors out

Filter

Hi, i have a question about a filter...

I wanna create a filter for example the request might be

name contains ´test´, bussiness equals ´worktest´

so with that i wanna create a query for filter information on my mongodb collection

any idea about this¿? someone please help me

tests passing without entering data

hey there! I'm having issues with getting tests to pass with the models exercises.

Steps taken

click to see my steps
  1. git clone url
  2. npm install
  3. navigate to the exercises/models directory and run this
  4. npm test exercises/models/__test__/
  5. tests pass even though I did not enter code for tests

on the connect.js file I entered this


const mongoose = require('mongoose')
mongoose.Promise = global.Promise

const connect = () => {
    return mongoose.connect('mongodb://localhost:27017/usersDatabase')
}
module.exports = connect

betaUser default

I have on the start user.js

betaUser: { type: Boolean, default: false },

like that but the test is not passing it? betaUser should default to false (11ms)

Here is the full schema

const mongoose = require('mongoose') const userSchema = new mongoose.Schema({ firstName: { type: String, required: true }, lastName: { type: String, required: true }, email: { type: String, required: true, unique: true }, betaUser: { type: Boolean, default: false }, birthDate: Date, address: { other: Boolean, street: String, houseNumber: Number, zip: Number, city: String, State: String }, pets: [{type: String}] }) module.exports = mongoose.model('user', userSchema)

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.