TIL031_210429
🍊 감상
📙 열품타 코딩 시간 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,
})
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
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))
Author And Source
이 문제에 관하여(TIL031_210429), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@keepcalm/TIL031210429저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)