const mongoose = require("mongoose");
mongoose.set("strictQuery", true);
mongoose
.connect("mongodb://localhost:27017/movieApp")
.then(() => {
console.log("Connection Open");
})
.catch((e) => {
console.log("ERROR");
console.log(e);
});
const movieSchema = new mongoose.Schema({
title: String,
year: Number,
score: Number,
rating: String,
});
const Movie = mongoose.model("Movie", movieSchema);
const amadeus = new Movie({
title: "Amadeus",
year: 1986,
score: 9.2,
rating: "R",
});
> brew services start mongodb-community
ERROR
## ERROR
> node
> .load index.js
대체 방법
## 대체 node -i -e "$(< 파일명.확장자)"
> node -i -e "$(< index.js)"
Node.js REPL(Read Eval Print Loop)
> node
- 사용자가 커맨드를 입력하면 시스템이 값을 반환하는 환경
- Read – 유저의 값을 입력 받아 JavaScript 데이터 구조로 메모리에 저장합니다.
- Eval – 데이터를 처리(Evaluate) 합니다.
- Print – 결과값을 출력합니다.
- Loop – Read, Eval, Print 를 유저가 Ctrl+C를 두번 눌러 종료할때까지 반복합니다.
- Node.js 의 REPL 환경은 자바스크립트 코드를 테스팅 및 디버깅할때 유용하게 사용
REPL 커맨드- Ctrl+C – 현재 명령어를 종료합니다.
- Ctrl+C (2번) – Node REPL 을 종료합니다.
- Ctrl+D – Node REPL을 종료합니다.
- 위/아래 키 – 명령어 히스토리를 탐색하고 이전 명령어를 수정합니다.
- Tab – 현재 입력란에 쓴 값으로 시작하는 명령어 / 변수 목록을 확인합니다.
- .help – 모든 커맨드 목록을 확인합니다.
- .break – 멀티 라인 표현식 입력 도중 입력을 종료합니다.
- .clear – .break 와 같습니다.
- .save filename – 현재 Node REPL 세션을 파일로 저장합니다.
- .load filename – Node REPL 세션을 파일에서 불러옵니다.
## (REPL)
> amadeus
{
title: 'Amadeus',
year: 1986,
score: 9,
rating: 'R',
_id: new ObjectId("63d62b67076bd51072fbff2f")
}
## New Termial
> mongo
## (mogo)
> use movieApp
switched to db movieApp
> db.movies.find()
>
## (REPL)
> amadeus.save()
Promise {
<pending>,
[Symbol(async_id_symbol)]: 1381,
[Symbol(trigger_async_id_symbol)]: 5
}
## (mongo)
> db.movies.find()
{ "_id" : ObjectId("63d62204c5072a854bc7aee5"), "title" : "Amadeus", "year" : 1986, "rating" : "R", "__v" : 0 }
> db.movies.find().pretty()
{
"_id" : ObjectId("63d62204c5072a854bc7aee5"),
"title" : "Amadeus",
"year" : 1986,
"rating" : "R",
"__v" : 0
}
## (REPL)
> amadeus.score = 9.5
9.5
> amadeus.save()
Promise {
<pending>,
[Symbol(async_id_symbol)]: 681,
[Symbol(trigger_async_id_symbol)]: 5
}
## (mongo)
> db.movies.find().pretty()
{
"_id" : ObjectId("63d63d433891e30b6cc9ed33"),
"title" : "Amadeus",
"year" : 1986,
"score" : 9.5,
"rating" : "R",
"__v" : 0
}
moogoose를 사용하면서 결과값을 보면 맨 밑에 쯤에
__v
가 있다.
"버전 키"라고 하는데 문서의 내부 개정판을 설명하고 기본값은 0이다.
mongoDB에서 바로 값을 넣으면 생기지 않지만 moogoose 통해 데이터가 삽입이 되면 생긴다.
__v
를 없애고 싶으면 스키마를 만드는 부분에{ versionKey : false }
를 추가해주면된다.const UserSchema = new mongoose.Schema({ userName: String, }, { versionKey: false });
insertMany()
를 호출하면 기본적으로 MongoDB에 바로 연결되어 한 번에 많이 입력 가능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("Worked");
console.log(data);
});
> node index.js
Connection Open
Worked
[
{
title: 'Amelie',
year: 2001,
score: 8.3,
rating: 'R',
_id: new ObjectId("63d6423aea01c752fde9afe8"),
__v: 0
},
{
title: 'Alien',
year: 1979,
score: 8.1,
rating: 'R',
_id: new ObjectId("63d6423aea01c752fde9afe9"),
__v: 0
},
{
title: 'The Iron Giant',
year: 1999,
score: 7.5,
rating: 'PG',
_id: new ObjectId("63d6423aea01c752fde9afea"),
__v: 0
},
{
title: 'Stand By Me',
year: 1986,
score: 8.6,
rating: 'R',
_id: new ObjectId("63d6423aea01c752fde9afeb"),
__v: 0
},
{
title: 'Moonrise Kingdom',
year: 2012,
score: 7.3,
rating: 'PG-13',
_id: new ObjectId("63d6423aea01c752fde9afec"),
__v: 0
}
]
## (mongo)
> db.movies.find().pretty()
{
"_id" : ObjectId("63d63d433891e30b6cc9ed33"),
"title" : "Amadeus",
"year" : 1986,
"score" : 9.5,
"rating" : "R",
"__v" : 0
}
{
"_id" : ObjectId("63d6423aea01c752fde9afe8"),
"title" : "Amelie",
"year" : 2001,
"score" : 8.3,
"rating" : "R",
"__v" : 0
}
{
"_id" : ObjectId("63d6423aea01c752fde9afe9"),
"title" : "Alien",
"year" : 1979,
"score" : 8.1,
"rating" : "R",
"__v" : 0
}
{
"_id" : ObjectId("63d6423aea01c752fde9afea"),
"title" : "The Iron Giant",
"year" : 1999,
"score" : 7.5,
"rating" : "PG",
"__v" : 0
}
{
"_id" : ObjectId("63d6423aea01c752fde9afeb"),
"title" : "Stand By Me",
"year" : 1986,
"score" : 8.6,
"rating" : "R",
"__v" : 0
}
{
"_id" : ObjectId("63d6423aea01c752fde9afec"),
"title" : "Moonrise Kingdom",
"year" : 2012,
"score" : 7.3,
"rating" : "PG-13",
"__v" : 0
}
> node -i -e "$(< index.js)"
## (REPL)
> Movie.find({}).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 669,
[Symbol(trigger_async_id_symbol)]: 667
}
> [
{
_id: new ObjectId("63d63d433891e30b6cc9ed33"),
title: 'Amadeus',
year: 1986,
score: 9.5,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d6423aea01c752fde9afe8"),
title: 'Amelie',
year: 2001,
score: 8.3,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d6423aea01c752fde9afe9"),
title: 'Alien',
year: 1979,
score: 8.1,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d6423aea01c752fde9afea"),
title: 'The Iron Giant',
year: 1999,
score: 7.5,
rating: 'PG',
__v: 0
},
{
_id: new ObjectId("63d6423aea01c752fde9afeb"),
title: 'Stand By Me',
year: 1986,
score: 8.6,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d6423aea01c752fde9afec"),
title: 'Moonrise Kingdom',
year: 2012,
score: 7.3,
rating: 'PG-13',
__v: 0
}
rating:"PG-13"
## (REPL)
> Movie.find({rating:"PG-13"}).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 932,
[Symbol(trigger_async_id_symbol)]: 930
}
> [
{
_id: new ObjectId("63d64990b7282db8d50ec60c"),
title: 'Moonrise Kingdom',
year: 2012,
score: 7.3,
rating: 'PG-13',
__v: 0
}
]
year: {$lt:1990}
## (REPL)
> Movie.find({year: {$lt:1990} }).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 1507,
[Symbol(trigger_async_id_symbol)]: 1505
}
> [
{
_id: new ObjectId("63d64990b7282db8d50ec609"),
title: 'Alien',
year: 1979,
score: 8.1,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60b"),
title: 'Stand By Me',
year: 1986,
score: 8.6,
rating: 'R',
__v: 0
}
]
findById()
특정 ID 찾기## (REPL)
> Movie.findById({_id:"63d64990b7282db8d50ec609"}).then(m => console.log(m))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 2090,
[Symbol(trigger_async_id_symbol)]: 2088
}
> {
_id: new ObjectId("63d64990b7282db8d50ec609"),
title: 'Alien',
year: 1979,
score: 8.1,
rating: 'R',
__v: 0
}
rating:"PG-13"
를 rating:"PG"
로 변경## (REPL)
> Movie.updateOne({rating:"PG-13"},{rating:"PG"}).then(res => console.log(res))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 4528,
[Symbol(trigger_async_id_symbol)]: 4526
}
> {
acknowledged: true,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1
}
## (REPL)
> Movie.find({rating:"PG"}).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 4838,
[Symbol(trigger_async_id_symbol)]: 4836
}
> [
{
_id: new ObjectId("63d64990b7282db8d50ec60a"),
title: 'The Iron Giant',
year: 1999,
score: 7.5,
rating: 'PG',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60c"),
title: 'Moonrise Kingdom',
year: 2012,
score: 7.3,
rating: 'PG',
__v: 0
}
]
findOneAndUpdate()
업데이트한 후 업데이트가 된 정보 보여주기## (REPL)
> Movie.findOneAndUpdate({title:"The Iron Giant"}, {score:7.321}, {new: true}).then(m => console.log(m))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 6256,
[Symbol(trigger_async_id_symbol)]: 6254
}
> {
_id: new ObjectId("63d64990b7282db8d50ec60a"),
title: 'The Iron Giant',
year: 1999,
score: 7.321,
rating: 'PG',
__v: 0
}
findOneAndUpdate()
의 세번째 인자인{new : true}
는 코드가 실행 됐을 때 나타날 정보를 판단한다.new
가true
라면 업데이트 한 후의 정보를 보여주고false
라면 업데이트 전의 정보를 보여준다.
title:"Amelie"
,title:"Alien"
의 score
를 10
으로 변경## (REPL)
> Movie.updateMany({title: {$in : ["Amelie", "Alien"]} }, {score : 10}).then(res => console.log(res))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 5669,
[Symbol(trigger_async_id_symbol)]: 5667
}
> {
acknowledged: true,
modifiedCount: 2,
upsertedId: null,
upsertedCount: 0,
matchedCount: 2
}
## result
> Movie.find({}).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 5723,
[Symbol(trigger_async_id_symbol)]: 5721
}
> [
{
_id: new ObjectId("63d64990b7282db8d50ec608"),
title: 'Amelie',
year: 2001,
score: 10,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec609"),
title: 'Alien',
year: 1979,
score: 10,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60a"),
title: 'The Iron Giant',
year: 1999,
score: 7.321,
rating: 'PG',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60b"),
title: 'Stand By Me',
year: 1986,
score: 8.6,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60c"),
title: 'Moonrise Kingdom',
year: 2012,
score: 7.3,
rating: 'PG',
__v: 0
}
]
title: "Amelie"
삭제## (REPL)
> Movie.remove({title: "Amelie"}).then(msg => console.log(msg))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 6891,
[Symbol(trigger_async_id_symbol)]: 6889
}
> (node:26118) [MONGODB DRIVER] Warning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
(Use `node --trace-warnings ...` to show where the warning was created)
{ acknowledged: true, deletedCount: 1 }
## result
> Movie.find({}).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 6976,
[Symbol(trigger_async_id_symbol)]: 6974
}
> [
{
_id: new ObjectId("63d64990b7282db8d50ec609"),
title: 'Alien',
year: 1979,
score: 10,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60a"),
title: 'The Iron Giant',
year: 1999,
score: 7.321,
rating: 'PG',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60b"),
title: 'Stand By Me',
year: 1986,
score: 8.6,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60c"),
title: 'Moonrise Kingdom',
year: 2012,
score: 7.3,
rating: 'PG',
__v: 0
}
]
- 위의 오류
> (node:26118) [MONGODB DRIVER] Warning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead. (Use `node --trace-warnings ...` to show where the warning was created) { acknowledged: true, deletedCount: 1 }
는
remove()
대신deleteOne()
,deleteMany()
,bulkWrite()
사용하라는 뜻
year
이 1999와 같거나 큰 데이터 삭제## (REPL)
> Movie.deleteMany({year: {$gte:1999}}).then(msg => console.log(msg))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 7599,
[Symbol(trigger_async_id_symbol)]: 7597
}
> { acknowledged: true, deletedCount: 2 }
## result
> Movie.find({}).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 7697,
[Symbol(trigger_async_id_symbol)]: 7695
}
> [
{
_id: new ObjectId("63d64990b7282db8d50ec609"),
title: 'Alien',
year: 1979,
score: 10,
rating: 'R',
__v: 0
},
{
_id: new ObjectId("63d64990b7282db8d50ec60b"),
title: 'Stand By Me',
year: 1986,
score: 8.6,
rating: 'R',
__v: 0
}
]
## (REPL)
> Movie.findOneAndDelete({title:"Alien"}).then(m => console.log(m))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 8187,
[Symbol(trigger_async_id_symbol)]: 8185
}
> {
_id: new ObjectId("63d64990b7282db8d50ec609"),
title: 'Alien',
year: 1979,
score: 10,
rating: 'R',
__v: 0
}
## result
> Movie.find({}).then(data => console.log(data))
Promise {
<pending>,
[Symbol(async_id_symbol)]: 8247,
[Symbol(trigger_async_id_symbol)]: 8245
}
> [
{
_id: new ObjectId("63d64990b7282db8d50ec60b"),
title: 'Stand By Me',
year: 1986,
score: 8.6,
rating: 'R',
__v: 0
}
]