/* 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
}