TIL031_210429

JIYOONΒ·2021λ…„ 5μ›” 1일
0

TIL

λͺ©λ‘ 보기
31/42
post-thumbnail

🍊 감상

πŸ“™ μ—΄ν’ˆνƒ€ μ½”λ”© μ‹œκ°„ 3hour
πŸ‘πŸΌ -
πŸ‘ŽπŸΌ -

πŸš€ λͺ©ν‘œ

  • Udemyμ—μ„œ Javascript κ°•μ’Œ μˆ˜κ°•ν•˜κΈ° (448/682)
  • 개인 ν”„λ‘œμ νŠΈ 진행

πŸ“£ The Web Developer Bootcamp 2021

37. Connecting to mongo with mongoose

380. Our first mongoose model

connect javascript file using mongoose to a mongo database.

const mongoose = require('mongoose');
mongoose
  .connect('mongodb://localhost:27017/movieApp', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })

central goal, the whole point of using mongoose is to have an eaiser way to interact with a mongo database from our javascript or from javascript land.

model

central thing in mongoose is the model
model: javascript classes that we make with the assistance of mongoose that represents the information in a mongo database, or specifically represents information in some collection.

schema

blueprint, a game plan.
mapping of different collection keys from mongo to different types in javascript.
We're taking data from mongo, which has different types that javascript or whatever language we're working in. By defining the schema, we can specify a` type of the key.

//define the schema - nothig to do with db
//concept on the javascript side of equation
const movieSchema = new mongoose.Schema({
  title: String,
  year: Number,
  score: Number,
  rating: String,
});

//tell mongoose that I want to make a model using that schema
//pass in a string containing the name of our model
//Name supposed to be singular and uppercase or capitalized the first letter
//mongoose is gonna take that and create a collection called movies
//It will pluralize(λ³΅μˆ˜ν™”) it and it will lowercase it
//So the collection is movies and the model name is Movie.
//save it to variable -> This gives us a class
//Now we have model class called Movie
const Movie = mongoose.model('Movie', movieSchema);
//make new instances of my movie class
const amadeus = new Movie({
  title: 'Amadeus',
  year: 1986,
  score: 9.2,
  rating: 'R',
});

in node shell

.load index.js -> μžλ°”μŠ€ν¬λ¦½νŠΈ μ•ˆμ— μžˆλŠ” 객체 확인
amadeus.save() -> take this data and migrate and save to mongo
amadeus.score = 9.5 -> dbμ—λŠ” 영ν–₯ μ•ˆ λ―ΈμΉ¨, javascript landμ—μ„œλ§Œ μˆ˜μ •λœ 것
amadeus.save() -> μˆ˜μ • 반영됨

in mongo shell
use movieApp
db.movies.find()

381. Insert Many

const blah = new Movie();
blah.save()
-> μ—¬λŸ¬ 개 λ§Œλ“€ λ•Œ 더 κ°„λ‹¨ν•˜κ²Œ ν•˜λŠ” 법

Movie.insertMany([
  { title: 'Amelie', year: 2001, score: 8.3, rating: 'R' },
  { title: 'Alien', year: 1979, score: 8.1, rating: 'R' },
  { title: 'The Iron Giant', year: 1999, score: 7.5, rating: 'PG' },
  { title: 'Stand By Me', year: 1986, score: 8.6, rating: 'R' },
  { title: 'Moonrise Kingdom', year: 2012, score: 7.3, rating: 'PG-13' }
])
  .then(data => {
    console.log(data)
    console.log("It worked")
  })
//we do not need to save with this process

382. Finding with mongoose

κ³΅μ‹λ¬Έμ„œ: mongoose

the result of find is not promise. It's something called mongoose query.
.then function is not a full-fledged promises -> considered thenable object

-> .exec()을 μ‚¬μš©ν•˜λŠ” 게 더 λ‚«λ‹€κ³  함 (λ‚˜μ€‘μ— 더 κ³΅λΆ€ν•˜κΈ°)

//in node shell 콜백 쿼리
Movie.find({})
-> returns a query object, which is a thenable object itself
query object does not give me the data I want right away

Movie.find({}).then(data=>console.log(data))
Movie.find({rating: 'PG-13'}).then(data=>console.log(data))
Movie.find({year: {$gte: 2015}}).then(data=>console.log(data))

Movie.find({__id: 'sei23345jknsli'}).then(d=>console.log(d))
Movie.fndById('sei23345jknsli').then(m=>console.log(m))

0개의 λŒ“κΈ€