* Practice1 Database, Movies Collection 사용) 3. Query / Find Documents(1) 모든 도큐먼트 찾기 */
use Practice1
db.movies.find()
/* 출력 */
switched to db Practice1
{
_id: ObjectId("6572d0649eb2afcbf945d552"),
title: 'Avatar'
}
{
_id: ObjectId("6572d0649eb2afcbf945d553"),
title: "Pee Wee Herman's Big Adventure"
}
{
_id: ObjectId("6572d0649eb2afcbf945d554"),
title: 'The Hobbit: An Unexpected Journey',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit'
}
{
_id: ObjectId("6572d0649eb2afcbf945d555"),
title: 'Pulp Fiction',
writer: 'Quentin Tarantino',
year: 1994,
actors: [
'John Travolta',
'Uma Thurman'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d556"),
title: 'Fight Club',
writer: 'Chuck Palahniuk',
year: 1999,
actors: [
'Brad Pitt',
'Edward Norton'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d557"),
title: 'The Hobbit: The Desolation of Smaug',
writer: 'J.R.R. Tolkein',
year: 2013,
franchise: 'The Hobbit'
}
{
_id: ObjectId("6572d0649eb2afcbf945d558"),
title: 'Inglorious Basterds',
writer: 'Quentin Tarantino',
year: 2009,
actors: [
'Brad Pitt',
'Diane Kruger',
'Eli Roth'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d559"),
title: 'The Hobbit: The Battle of the Five Armies',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness.'
}
/* 3. Query / Find Documents(2) writer "쿠엔틴 타란티노"로 설정된 모든 문서 찾기 */
db.movies.find({writer:'Quentin Tarantino'})
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d555"),
title: 'Pulp Fiction',
writer: 'Quentin Tarantino',
year: 1994,
actors: [
'John Travolta',
'Uma Thurman'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d558"),
title: 'Inglorious Basterds',
writer: 'Quentin Tarantino',
year: 2009,
actors: [
'Brad Pitt',
'Diane Kruger',
'Eli Roth'
]
}
/* 3. Query / Find Documents(3) actors "브래드 피트"가 포함된 모든 문서 찾기 */
db.movies.find({actors:'Brad Pitt'})
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d556"),
title: 'Fight Club',
writer: 'Chuck Palahniuk',
year: 1999,
actors: [
'Brad Pitt',
'Edward Norton'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d558"),
title: 'Inglorious Basterds',
writer: 'Quentin Tarantino',
year: 2009,
actors: [
'Brad Pitt',
'Diane Kruger',
'Eli Roth'
]
}
/* 3. Query / Find Documents(4) franchise "호빗"으로 설정된 모든 문서 찾기 */
db.movies.find({franchise: 'The Hobbit'})
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d554"),
title: 'The Hobbit: An Unexpected Journey',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit'
}
{
_id: ObjectId("6572d0649eb2afcbf945d557"),
title: 'The Hobbit: The Desolation of Smaug',
writer: 'J.R.R. Tolkein',
year: 2013,
franchise: 'The Hobbit'
}
{
_id: ObjectId("6572d0649eb2afcbf945d559"),
title: 'The Hobbit: The Battle of the Five Armies',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness.'
}
/* 3. Query / Find Documents(5) 90년대 개봉 영화 모두 찾기 */
db.movies.find({year: {$lt:2000} })
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d555"),
title: 'Pulp Fiction',
writer: 'Quentin Tarantino',
year: 1994,
actors: [
'John Travolta',
'Uma Thurman'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d556"),
title: 'Fight Club',
writer: 'Chuck Palahniuk',
year: 1999,
actors: [
'Brad Pitt',
'Edward Norton'
]
}
/* 3. Query / Find Documents(6) 2000년 이전 또는 2010년 이후 개봉된 영화 모두 찾기 */
db.movies.find({$or: [{year: {$lt:2000} }, {year: {$gt:2010} }] })
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d554"),
title: 'The Hobbit: An Unexpected Journey',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit'
}
{
_id: ObjectId("6572d0649eb2afcbf945d555"),
title: 'Pulp Fiction',
writer: 'Quentin Tarantino',
year: 1994,
actors: [
'John Travolta',
'Uma Thurman'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d556"),
title: 'Fight Club',
writer: 'Chuck Palahniuk',
year: 1999,
actors: [
'Brad Pitt',
'Edward Norton'
]
}
{
_id: ObjectId("6572d0649eb2afcbf945d557"),
title: 'The Hobbit: The Desolation of Smaug',
writer: 'J.R.R. Tolkein',
year: 2013,
franchise: 'The Hobbit'
}
{
_id: ObjectId("6572d0649eb2afcbf945d559"),
title: 'The Hobbit: The Battle of the Five Armies',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness.'
}
/* 4. Update Documents(1) "The Hobbit: An Unexpected Journey"에 synopsis 추가 */
db.movies.updateOne({title:"The Hobbit: An Unexpected Journey"}, {$set: {synopsis: "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}})
/* 출력 */
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
/* 4. Update Documents(2) "The Hobbit: The Desolation of Smaug"에 synopsis 추가 */
db.movies.updateOne({title:"The Hobbit: The Desolation of Smaug"}, {$set: {synopsis: "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."} })
/* 출력 */
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
/* 4. Update Documents(3) "Pulp Fiction"의 actors에 "Samuel L. Jackson"을 추가 */
db.movies.updateOne({title: "Pulp Fiction"}, {$addToSet: {actors: {$each: ['Samuel L. Jackson'] } } })
/* 출력 */
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
/* 5. Text Search(1) "Bilbo"라는 단어가 포함된 시놉시스가 있는 모든 영화 찾기 */
db.movies.createIndex({synopsis:"text"})
db.movies.find( {$text: {$search: "Bilbo"} } )
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d557"),
title: 'The Hobbit: The Desolation of Smaug',
writer: 'J.R.R. Tolkein',
year: 2013,
franchise: 'The Hobbit',
synopsis: 'The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring.'
}
{
_id: ObjectId("6572d0649eb2afcbf945d559"),
title: 'The Hobbit: The Battle of the Five Armies',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness.'
}
{
_id: ObjectId("6572d0649eb2afcbf945d554"),
title: 'The Hobbit: An Unexpected Journey',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug.'
}
/* 5. Text Search(2) "Gandalf"라는 단어가 포함된 시놉시스가 있는 모든 영화 찾기 */
db.movies.find( {$text: {$search:"Gandalf"} } )
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d557"),
title: 'The Hobbit: The Desolation of Smaug',
writer: 'J.R.R. Tolkein',
year: 2013,
franchise: 'The Hobbit',
synopsis: 'The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring.'
}
/* 5. Text Search(3) "Gandalf" 제외, "Bilbo" 단어가 포함된 시놉시스가 있는 모든 영화 찾기 */
db.movies.find( { $text: { $search: "Bilbo -Gandalf" } } )
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d559"),
title: 'The Hobbit: The Battle of the Five Armies',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness.'
}
{
_id: ObjectId("6572d0649eb2afcbf945d554"),
title: 'The Hobbit: An Unexpected Journey',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug.'
}
/* 5. Text Search(4) "dwarves" 또는 "hobbit" 단어가 포함된 시놉시스가 있는 모든 영화 찾기 */
db.movies.find( { $text: { $search: "dwarves hobbit" } } )
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d554"),
title: 'The Hobbit: An Unexpected Journey',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug.'
}
{
_id: ObjectId("6572d0649eb2afcbf945d557"),
title: 'The Hobbit: The Desolation of Smaug',
writer: 'J.R.R. Tolkein',
year: 2013,
franchise: 'The Hobbit',
synopsis: 'The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring.'
}
/* 5. Text Search(5) "gold"와 "dragon" 단어가 포함된 시놉시스가 있는 모든 영화 찾기 */
db.movies.find( { $text: { $search: '"gold" "dragon"' } } )
/* 출력 */
{
_id: ObjectId("6572d0649eb2afcbf945d554"),
title: 'The Hobbit: An Unexpected Journey',
writer: 'J.R.R. Tolkein',
year: 2012,
franchise: 'The Hobbit',
synopsis: 'A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug.'
}
/* 6. Delete Documents(1) delete the movie "Pee Wee Herman's Big Adventure" */
db.movies.remove({title:"Pee Wee Herman's Big Adventure"})
/* 출력 */
{
acknowledged: true,
deletedCount: 1
}
/* 6. Delete Documents(2) delete the movie "Avatar" */
db.movies.remove({title: "Avatar"})
/* 출력 */
{
acknowledged: true,
deletedCount: 1
}
/* 7. Relationships(1) users collection documents insert */
db.users.insertMany([
{
"username" : "ScumbagSteve",
"full_name" : '',
"first" : "Scumbag",
"last" : "Steve"
},
{
"username" : "GoodGuyGreg",
"first_name" : "Good Guy",
"last_name" : "Greg"
}
]);
/* 7. Relationships(2) posts collection documents insert */
db.posts.insertMany([
{
'username' : 'GoodGuyGreg',
'title' : 'Passes out at party',
'body' : 'Wakes up early and cleans house'
},
{
'username' : 'GoodGuyGreg',
'title' : 'Steals your identity',
'body' : 'Raises your credit score'
},
{
'username' : 'GoodGuyGreg',
'title' : 'Reports a bug in your code',
'body' : 'Sends you a Pull Request'
},
{
'username' : 'ScumbagSteve',
'title' : 'Borrows something',
'body' : 'Sells it'
},
{
'username' : 'ScumbagSteve',
'title' : 'Borrows everything',
'body' : 'The end'
},
{
'username' : 'ScumbagSteve',
'title' : 'Forks your repo on github',
'body' : 'Sets to private'
}
]);
/* 7. Relationships(3) comments collection documents insert */
var pass = ObjectId("657a88c8da631410b49452be");
var reports = ObjectId("657a88c8da631410b49452c0");
var borsth = ObjectId("657a88c8da631410b49452c1");
var borall = ObjectId("657a88c8da631410b49452c2");
var forks = ObjectId("657a88c8da631410b49452c3");
db.comments.insertMany([
{
"username" : "GoodGuyGreg",
"comment" : "Hope you got a good deal!",
"post" : borsth
},
{
"username" : "GoodGuyGreg",
"comment" : "What's mine is yours!",
"post" : borall
},
{
"username" : "GoodGuyGreg",
"comment" : "Don't violate the licensing agreement!",
"post" : forks
},
{
"username" : "ScumbagSteve",
"comment" : "It still isn't clean",
"post" : pass
},
{
"username" : "ScumbagSteve",
"comment" : "Denied your PR cause I found a hack",
"post" : reports
}
]);
/* 8. Querying related collections(1) 모든 user 찾기 */
db.users.find()
/* 출력 */
{
_id: ObjectId("657a87adda631410b49452bc"),
username: 'GoodGuyGreg',
first_name: 'Good Guy',
last_name: 'Greg'
}
{
_id: ObjectId("657a87adda631410b49452bd"),
username: 'ScumbagSteve',
full_name: '',
first: 'Scumbag',
last: 'Steve'
}
/* 8. Querying related collections(2) 모든 posts 찾기 */
db.posts.find()
/* 출력 */
{
_id: ObjectId("657a88c8da631410b49452be"),
username: 'GoodGuyGreg',
title: 'Passes out at party',
body: 'Wakes up early and cleans house'
}
{
_id: ObjectId("657a88c8da631410b49452bf"),
username: 'GoodGuyGreg',
title: 'Steals your identity',
body: 'Raises your credit score'
}
{
_id: ObjectId("657a88c8da631410b49452c0"),
username: 'GoodGuyGreg',
title: 'Reports a bug in your code',
body: 'Sends you a Pull Request'
}
{
_id: ObjectId("657a88c8da631410b49452c1"),
username: 'ScumbagSteve',
title: 'Borrows something',
body: 'Sells it'
}
{
_id: ObjectId("657a88c8da631410b49452c2"),
username: 'ScumbagSteve',
title: 'Borrows everything',
body: 'The end'
}
{
_id: ObjectId("657a88c8da631410b49452c3"),
username: 'ScumbagSteve',
title: 'Forks your repo on github',
body: 'Sets to private'
}
/* 8. Querying related collections(3) "GoodGuyGreg"가 작성한 모든 게시물 찾기 */
db.posts.find({username: 'GoodGuyGreg'})
/* 출력 */
{
_id: ObjectId("657a88c8da631410b49452be"),
username: 'GoodGuyGreg',
title: 'Passes out at party',
body: 'Wakes up early and cleans house'
}
{
_id: ObjectId("657a88c8da631410b49452bf"),
username: 'GoodGuyGreg',
title: 'Steals your identity',
body: 'Raises your credit score'
}
{
_id: ObjectId("657a88c8da631410b49452c0"),
username: 'GoodGuyGreg',
title: 'Reports a bug in your code',
body: 'Sends you a Pull Request'
}
/* 8. Querying related collections(4) "ScumbagSteve"가 작성한 모든 게시물 찾기 */
db.posts.find({username: "ScumbagSteve"})
/* 출력 */
{
_id: ObjectId("657a88c8da631410b49452c1"),
username: 'ScumbagSteve',
title: 'Borrows something',
body: 'Sells it'
}
{
_id: ObjectId("657a88c8da631410b49452c2"),
username: 'ScumbagSteve',
title: 'Borrows everything',
body: 'The end'
}
{
_id: ObjectId("657a88c8da631410b49452c3"),
username: 'ScumbagSteve',
title: 'Forks your repo on github',
body: 'Sets to private'
}
/* 8. Querying related collections(5) 모든 comments 찾기 */
db.comments.find()
/* 출력 */
{
_id: ObjectId("657a8dcbda631410b49452c9"),
username: 'GoodGuyGreg',
comment: 'Hope you got a good deal!',
post: ObjectId("657a88c8da631410b49452c1")
}
{
_id: ObjectId("657a8dcbda631410b49452ca"),
username: 'GoodGuyGreg',
comment: "What's mine is yours!",
post: ObjectId("657a88c8da631410b49452c2")
}
{
_id: ObjectId("657a8dcbda631410b49452cb"),
username: 'GoodGuyGreg',
comment: "Don't violate the licensing agreement!",
post: ObjectId("657a88c8da631410b49452c3")
}
{
_id: ObjectId("657a8dcbda631410b49452cc"),
username: 'ScumbagSteve',
comment: "It still isn't clean",
post: ObjectId("657a88c8da631410b49452be")
}
{
_id: ObjectId("657a8dcbda631410b49452cd"),
username: 'ScumbagSteve',
comment: 'Denied your PR cause I found a hack',
post: ObjectId("657a88c8da631410b49452c0")
}
/* 8. Querying related collections(6) "GoodGuyGreg"가 작성한 모든 댓글 찾기 */
db.comments.find({username:"GoodGuyGreg"})
/* 출력 */
{
_id: ObjectId("657a8dcbda631410b49452c9"),
username: 'GoodGuyGreg',
comment: 'Hope you got a good deal!',
post: ObjectId("657a88c8da631410b49452c1")
}
{
_id: ObjectId("657a8dcbda631410b49452ca"),
username: 'GoodGuyGreg',
comment: "What's mine is yours!",
post: ObjectId("657a88c8da631410b49452c2")
}
{
_id: ObjectId("657a8dcbda631410b49452cb"),
username: 'GoodGuyGreg',
comment: "Don't violate the licensing agreement!",
post: ObjectId("657a88c8da631410b49452c3")
}
/* 8. Querying related collections(7) "ScumbagSteve"가 작성한 모든 댓글 찾기 */
db.comments.find({username:"ScumbagSteve"})
/* 출력 */
{
_id: ObjectId("657a8dcbda631410b49452cc"),
username: 'ScumbagSteve',
comment: "It still isn't clean",
post: ObjectId("657a88c8da631410b49452be")
}
{
_id: ObjectId("657a8dcbda631410b49452cd"),
username: 'ScumbagSteve',
comment: 'Denied your PR cause I found a hack',
post: ObjectId("657a88c8da631410b49452c0")
}
/* 8. Querying related collections(8) "Reports a bug in your code" 게시물에 속한 모든 댓글 찾기 */
db.comments.find({post: reports})
/* 출력 */
{
_id: ObjectId("657a8dcbda631410b49452cd"),
username: 'ScumbagSteve',
comment: 'Denied your PR cause I found a hack',
post: ObjectId("657a88c8da631410b49452c0") // 'Reports a bug in your code' 게시물의 ObjectId
}
/* 9 CSV file을 populations collection에 import */
mongoimport --db Practice1 --collection populations --file Practice_1.csv
/* 9-1 인구가 10000명 이상인 도시의 이름과 인구를 표시 */
db.populations.find({ pop: {$gte: 10000} }, {_id: 0, city:1, name:1, pop:1})
/* 9-2 표시된 도시를 기준으로 주의 이름과 인구를 표시 */
db.populations.find({ pop: {$gte: 10000} }, {_id: 0, state:1, pop:1})
/* 9-3 NY의 전체 도시를 '인구'가 보이도록 표시 */
db.populations.find({state:'NY'}, {_id: 0, pop:1})
/* 출력 */
{
pop: 18913
}
{
pop: 943
}
{
pop: 958
}
{
pop: 2867
}
/* 9-4 인구가 20,000명이 넘는 곳 _id, 도시, 이름 표시 */
db.populations.find({ pop: {$gt: 20000} }, {_id: 1, city:1, name:1})
/* 출력 */
{
_id: '02907',
city: 'CRANSTON'
}
{
_id: 'NY',
name: 'New York'
}
{
_id: '33125',
city: 'MIAMI'
}
{
_id: '02906',
city: 'PROVIDENCE'
}
{
_id: 'MA',
name: 'Massachusetts'
}
{
_id: 'RI',
name: 'Rhode Island'
}
{
_id: 'FL',
name: 'Florida'
}