Trong collection posts, dùng aggregation để tìm các bài viết có mảng tags tồn tại và có phần tử trùng nhau (một tag xuất hiện từ 2 lần trở lên trong cùng mảng).
Gợi ý: so sánh $size của mảng tags gốc với số tag khác nhau (tính bằng $setUnion hoặc $addToSet).
Yêu cầu hiển thị:
title và tags (mảng tags gốc, giữ nguyên thứ tự và các phần tử trùng); không hiển thị _id._id tăng dần.use.Collection posts (dữ liệu mẫu sample_training.posts) đã được nạp sẵn.
Kết quả của câu aggregate theo định dạng mongosh: một mảng các document dạng { title: ..., tags: [ ... ] } (thứ tự trường title rồi tags). Ví dụ phần đầu:
[
{
title: 'Bill of Rights',
tags: [
'jasmine', 'buzzard',
...
Ví dụ:
Đầu vào:
sample_training.posts
Đầu ra:
[
{
title: 'Bill of Rights',
tags: [
'jasmine', 'buzzard',
'pink', 'liver',
'balinese', 'scorpio',
'saxophone', 'buzzard',
'option', 'berry'
]
},
{
title: 'US Constitution',
tags: [
'flight', 'wall',
'city', 'flight',
'sock', 'aquarius',
'mistake', 'flame',
'reaction', 'support'
]
},
{
title: 'Bill of Rights',
tags: [
'venezuela', 'interviewer',
'crown', 'knot',
'mattock', 'secure',
'guilty', 'venezuela',
'woolen', 'dock'
]
},
{
title: 'Bill of Rights',
tags: [
'hood', 'ladybug',
'collision', 'veil',
'hell', 'employee',
'shield', 'beach',
'collision', 'motorboat'
]
},
{
title: 'Gettysburg Address',
tags: [
'owner', 'comb',
'jaguar', 'cloth',
'frame', 'key',
'fiction', 'example',
'key', 'time'
]
},
{
title: 'Declaration of Independence',
tags: [
'bomb', 'layer',
'crowd', 'diaphragm',
'layer', 'hot',
'spruce', 'cardboard',
'lift', 'airport'
]
}
]Đang tải editor...