Mỗi document trong grades có mảng scores gồm các phần tử { type, score }.
Thực hiện các bước sau:
• Tạo index (tăng dần) cho trường scores.score
• Dùng aggregation: $unwind mảng scores để mỗi điểm thành một document, $match giữ các điểm có scores.score < 50, sắp xếp theo scores.score giảm dần, lấy 10 kết quả đầu tiên
• Chỉ in ra: student_id và score (giá trị của scores.score), không hiển thị _id
(Có thể đặt thêm một $match trước $unwind để tận dụng index.) Chương trình in đúng hai thứ: tên index vừa tạo và kết quả aggregation (không cần lệnh use).
Collection grades (dữ liệu mẫu sample_training.grades) đã được nạp sẵn.
Dòng đầu là tên index, tiếp theo là mảng 10 document theo định dạng mongosh, ví dụ:
scores.score_1
[
{ student_id: 4416, score: 49.999996860482796 },
...
]
Ví dụ:
Đầu vào:
sample_training.grades
Đầu ra:
scores.score_1
[
{ student_id: 4416, score: 49.999996860482796 },
{ student_id: 2275, score: 49.99985289981661 },
{ student_id: 3192, score: 49.99977859324505 },
{ student_id: 6714, score: 49.9996696294082 },
{ student_id: 6676, score: 49.99945686286993 },
{ student_id: 1336, score: 49.99892996770821 },
{ student_id: 2891, score: 49.99841087860024 },
{ student_id: 7018, score: 49.99838010001171 },
{ student_id: 401, score: 49.998176371695955 },
{ student_id: 5133, score: 49.99783799486146 }
]Đang tải editor...