Xác định thời điểm bình luận mới nhất cho từng phim trong collection comments:
• Gom nhóm theo movie_id (giá trị nhóm nằm ở _id)
• Lấy giá trị lớn nhất của trường date, đặt tên trường kết quả là latestDate
• Sắp xếp kết quả theo latestDate giảm dần (mới nhất trước); nếu bằng nhau thì theo _id tăng dần
• Chỉ lấy 10 phim đứng đầu
Không cần lệnh use. Chương trình chỉ được in kết quả của câu aggregate này.
Collection comments (dữ liệu mẫu sample_mflix.comments) đã được nạp sẵn.
Kết quả của câu aggregate theo định dạng mongosh: một mảng 10 document dạng { _id: <movie_id>, latestDate: <ISODate> }. Ví dụ phần đầu:
[
{
_id: ObjectId('573a13eff29313caabdd82f3'),
latestDate: ISODate('2018-08-14T15:05:03.142Z')
},
...
]
Ví dụ:
Đầu vào:
sample_mflix.comments
Đầu ra:
[
{
_id: ObjectId('573a13eff29313caabdd82f3'),
latestDate: ISODate('2018-08-14T15:05:03.142Z')
},
{
_id: ObjectId('573a1394f29313caabce08c8'),
latestDate: ISODate('2017-09-13T00:37:11.000Z')
},
{
_id: ObjectId('573a1399f29313caabcedc5c'),
latestDate: ISODate('2017-09-12T08:32:53.000Z')
},
{
_id: ObjectId('573a1396f29313caabce4d2f'),
latestDate: ISODate('2017-09-11T23:10:35.000Z')
},
{
_id: ObjectId('573a13adf29313caabd2b4c8'),
latestDate: ISODate('2017-09-11T16:52:51.000Z')
},
{
_id: ObjectId('573a1395f29313caabce2c23'),
latestDate: ISODate('2017-09-11T13:24:21.000Z')
},
{
_id: ObjectId('573a13b7f29313caabd4b0d4'),
latestDate: ISODate('2017-09-10T07:56:58.000Z')
},
{
_id: ObjectId('573a13b3f29313caabd3b647'),
latestDate: ISODate('2017-09-10T06:24:48.000Z')
},
{
_id: ObjectId('573a13acf29313caabd28635'),
latestDate: ISODate('2017-09-09T16:58:34.000Z')
},
{
_id: ObjectId('573a13b0f29313caabd3573a'),
latestDate: ISODate('2017-09-09T13:16:33.000Z')
}
]Đang tải editor...