π μ΄νν μ½λ© μκ° 3hour
ππΌ -
ππΌ -
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()
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
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))