ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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 파일로 작성 후 'test.json'으로 저장
    mongoimport 명령어를 이용해 grades 이름을 가진 collection 안에 json 파일 import */
    /* shell 창 아닌 cmd 창에서 입력 */
    mongoimport -d test -c grades --drop --file test.json --jsonArray
    
    /* 5개 document 입력된 것 확인 */
    2023-12-08T15:12:19.086+0900    connected to: mongodb://localhost/
    2023-12-08T15:12:19.099+0900    dropping: test.grades
    2023-12-08T15:12:19.109+0900    5 document(s) imported successfully. 0 document(s) failed to import.

    /* 3. grades collection) 65보다 크거나 같은 score 오름차순 정렬,
    그중 가장 낮은 score를 가진 사람 student_id 구하기 */
    db.grades.find({score: {$gte: 65}}, {student_id: true}).sort({score: 1}).limit(1)
    
    /* 출력 */
    {
      _id: ObjectId("6572b3c381e1296be241e20d"),
      student_id: 5
    }
    
    /* 4. grades collection) score만 출력하는 find() 메서드 작성 */
    db.grades.find({}, {score: true, _id: false})
    
    /* 출력 */
    {
      score: 88
    }
    {
      score: 60
    }
    {
      score: 95
    }
    {
      score: 92
    }
    {
      score: 50
    }

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

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