ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 배열 연습 문제
    데이터베이스/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점 이상인 학생 찾기

    db.class.find({grades:{$elemMatch:{subject:'과학', score:{$gte:90}}}}).pretty()
    db.class.find({"grades.subject": "과학", "grades.score": {$gte : 90}})
    
    {
      _id: 1,
      name: '반장',
      grades: [
        {
          subject: '수학',
          score: 88
        },
        {
          subject: '과학',
          score: 90
        }
      ]
    }
    {
      _id: 2,
      name: '부반장',
      grades: [
        {
          subject: '수학',
          score: 88
        },
        {
          subject: '과학',
          score: 90
        }
      ]
    }
    {
      _id: 3,
      name: '서기',
      grades: [
        {
          subject: '수학',
          score: 85
        },
        {
          subject: '과학',
          score: 100
        }
      ]
    }

     

     

    3. 수학 성적이 88점인 학생과 과학 성적이 100점인 학생 모두 찾기

    db.class.find({$or: [{grades:{$elemMatch:{subject:'수학', score:88}}}, {grades:{$elemMatch:{subject:'과학', score:100}}}]})
    db.class.find({$or: [{'grades.subject': '수학', 'grades.score': 88}, {'grades.subject':'과학', 'grades.score':100}]})
    {
      _id: 1,
      name: '반장',
      grades: [
        {
          subject: '수학',
          score: 88
        },
        {
          subject: '과학',
          score: 90
        }
      ]
    }
    {
      _id: 2,
      name: '부반장',
      grades: [
        {
          subject: '수학',
          score: 88
        },
        {
          subject: '과학',
          score: 90
        }
      ]
    }
    {
      _id: 3,
      name: '서기',
      grades: [
        {
          subject: '수학',
          score: 85
        },
        {
          subject: '과학',
          score: 100
        }
      ]
    }

     

    4. 모든 성적에서 90점 이상을 받은 학생 찾기

    db.class.find({$and: [{'grades.0.score': {$gte: 90}}, {'grades.1.score': {$gte: 90}}]})

     

    5. 성적 배열에서 첫 번째 성적이 90점 미만이고, 두 번째 성적이 95점 이상인 학생 찾기

    db.class.find({$and: [{'grades.0.score': {$lt: 90}}, {'grades.1.score': {$gte: 95}}]})
    
    {
      _id: 3,
      name: '서기',
      grades: [
        {
          subject: '수학',
          score: 85
        },
        {
          subject: '과학',
          score: 100
        }
      ]
    }

     

     

     

    6. 전자기기 태그가 있는 모든 제품 찾기

    db.product.find({tags: '전자기기'})

     

    7. 스토리지 사양이 256GB인 제품 찾기

    db.product.find({'specifications.name': '스토리지', 'specifications.value':'256GB'})
    
    {
      _id: 2,
      product: '스마트폰',
      tags: [
        '전자기기',
        '통신기기',
        '휴대용'
      ],
      specifications: [
        {
          name: 'CPU',
          value: 'Snapdragon 888'
        },
        {
          name: '메모리',
          value: '8GB'
        },
        {
          name: '스토리지',
          value: '256GB'
        }
      ]
    }

     

    8. 휴대용 태그를 가지고 있으면서 메모리 사양이 16GB 이상인 제품 찾기

     

    9. 태그 배열에 컴퓨터와 휴대용이 모두 포함된 제품 찾기

    db.product.find({$and:[{tags:'컴퓨터'}, {tags:'휴대용'}]})
    
    {
      _id: 1,
      product: '노트북',
      tags: [
        '전자기기',
        '컴퓨터',
        '휴대용'
      ],
      specifications: [
        {
          name: 'CPU',
          value: 'Intel i7'
        },
        {
          name: '메모리',
          value: '16GB'
        },
        {
          name: '스토리지',
          value: '512GB SSD'
        }
      ]
    }

     

    10. 스토리지 사양이 SSD인 제품 찾기

    db.product.find({specifications:
                        {$elemMatch:
                            {name: "스토리지",
                             value: {$regex: "SSD"}}}
    })
    
    {
      _id: 1,
      product: '노트북',
      tags: [
        '전자기기',
        '컴퓨터',
        '휴대용'
      ],
      specifications: [
        {
          name: 'CPU',
          value: 'Intel i7'
        },
        {
          name: '메모리',
          value: '16GB'
        },
        {
          name: '스토리지',
          value: '512GB SSD'
        }
      ]
    }
    {
      _id: 3,
      product: '데스크탑',
      tags: [
        '전자기기',
        '컴퓨터'
      ],
      specifications: [
        {
          name: 'CPU',
          value: 'AMD Ryzen 7'
        },
        {
          name: '메모리',
          value: '32GB'
        },
        {
          name: '스토리지',
          value: '1TB SSD'
        }
      ]
    }

    '데이터베이스 > MongoDB' 카테고리의 다른 글

    배열 연산자  (0) 2023.12.28
    문자열 연산자  (0) 2023.12.28
    MongoDB 문제 예제 2  (0) 2023.12.14
    MongoDB 문제 예제 1  (0) 2023.12.14
    MongoDB 셸 명령어 활용  (0) 2023.11.16
Designed by Tistory.