데이터베이스/MongoDB
-
프로젝션 연산자데이터베이스/MongoDB 2023. 12. 29. 11:12
배열 필드 프로젝션 db.inventory2.find({}, {'tags.0': 1}) tags 필드에 첫 번째 요소(첫 번째 인덱스)가 나타나지 않음 예시 데이터 입력 db.stock.insertMany([ {item: 'journal', instock: [ {warehouse: 'A', qty: 5}, {warehouse: 'C', qty: 15} ]}, {item: 'notebook', instock: [ {warehouse: 'C', qty: 5} ]}, {item: 'paper', instock: [ {warehouse: 'A', qty: 60}, {warehouse: 'B', qty: 15} ]}, {item: 'planner', instock: [ {warehouse: 'A', qty: 40..
-
쿼리 작성 실습데이터베이스/MongoDB 2023. 12. 28. 13:50
실습 예제 준비 1. 사이트에서 다운받기 https://github.com/Karoid/mongodb_tutorials GitHub - Karoid/mongodb_tutorials: 몽고디비 실습을 위한 자료 몽고디비 실습을 위한 자료. Contribute to Karoid/mongodb_tutorials development by creating an account on GitHub. github.com 2. cmd 창에서 입력 (git이 다운로드되어 있어야 함) > git clone https://github.com/Karoid/mongodb_tutorials.git > cd mongodb_tutorials > cd car_accident > mongoimport -d car_accident -c a..
-
배열 연산자데이터베이스/MongoDB 2023. 12. 28. 13:50
# 데이터 입력 db.inventory2.insertMany([ { item: 'journal', qty: 25, tags: ['blank', 'red'] }, { item: 'notebook', qty: 50, tags: ['red', 'blank'] }, { item: 'paper', qty: 100, tags: ['red', 'blank', 'plain']}, { item: 'planner', qty: 75, tags: ['blank', 'red'] }, { item: 'postcard', qty: 45, tags: ['blue']}]) # red 문자열이 들어있는 도큐먼트 모두 검색됨 db.inventory2.find({tags: 'red'}, {_id: 0}) { item: 'journal', ..
-
문자열 연산자데이터베이스/MongoDB 2023. 12. 28. 11:54
◎ $regex 연산자 값이 정규 표현식과 맞는 도큐먼트를 선택 $regex 연산자는 옵션 값을 설정할 수 있어 다른 연산자와는 형식이 조금 다름 # 연산자의 형식 3가지 { : {$regex: /pattern/, $options: ''} } { : {$regex: 'pattern', $options: ''} } { : {$regex: /pattern/} } 옵션 설명 i 대소문자 무시 m 정규식에서 앵커(^)를 사용할 때 값에 \n이 있다면 무력화 x 정규식 안에 있는 공백을 모두 무시 s 점(.)을 사용할 때 \n을 포함해서 매치 자리에는 표의 네 가지 문자를 넣을 수 있음 ◎ $text 연산자 $text 연산자는 해당 컬렉션의 텍스트 인덱스 안에서만 작동 db.stores.insertMany( [ ..
-
배열 연습 문제데이터베이스/MongoDB 2023. 12. 15. 16:06
1. 첫 번째 과목이 수학이고, 점수가 85보다 큰 학생 찾기 db.class.find({grades:{$elemMatch:{subject:'수학', score:{$gt:85}}}}).pretty() db.class.find({'grades.subject': "수학", 'grades.score': {$gte : 85}}) { _id: 1, name: '반장', grades: [ { subject: '수학', score: 88 }, { subject: '과학', score: 90 } ] } { _id: 2, name: '부반장', grades: [ { subject: '수학', score: 88 }, { subject: '과학', score: 90 } ] } 2. 두 번째 과목에서 과학 점수가 90점 이상..
-
MongoDB 문제 예제 2데이터베이스/MongoDB 2023. 12. 14. 15:02
* 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:..
-
MongoDB 문제 예제 1데이터베이스/MongoDB 2023. 12. 14. 14:58
/* 1. test1DB의 Student 컬렉션에서 name: 'Greg Powell', _id 포함, Score 제외하는 결과 출력 */ db.Student.find({name:'Greg Powell'}, {_id:true, score:false}); /* 출력 */ { _id: ObjectId("65558957872dd38face7b3b6"), name: 'Greg Powell', email: 'greg powell@fake-mail.com', version: 1, scores: [ 65, 75, 80 ], dateCreated: 1999-02-10T00:00:00.000Z } /* 2. student_id, type과 score를 갖는 임의의 5개 document를 json 파일로 작성 후 'tes..
-