Try these Array Operations:
db.posts.find({}, {_id:0, category:1, tags:1});
db.posts.updateMany({category:'Event'}, {$addToSet:{tags:"Tech"}});
db.posts.updateMany({category:'Event'}, {$push:{tags:"Tech Event"}});
db.posts.updateMany({category:'Event'}, {$pull:{tags:"Tech"}});
db.posts.updateOne( { likes: 4 }, { $pop: { tags: -1 } } )
db.posts.updateMany({}, {$pull:{tags:["Tech"]}});
db.posts.updateMany({}, {$pull:{tags:"Tech"}});
db.posts.updateMany({}, {$pull:{tags:{$in:["Tech", "Tech Event"]}}});
Run it several times: db.posts.updateMany({}, {$push:{tags:"Tech Event"}});
Check data: db.posts.find({}, {tags:1, _id:0});
Pull and then check data:
db.posts.updateMany({}, {$pull:{tags:{$in:["Tech", "Tech Event"]}}});
db.posts.find({}, {tags:1, _id:0});
Use data from the URL below: https://www.w3schools.com/mongodb/mongodb_mongosh_insert.php
db.posts.insertMany([
{
title: "Post Title 2",
body: "Body of post.",
category: "Event",
likes: 2,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 3",
body: "Body of post.",
category: "Technology",
likes: 3,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 4",
body: "Body of post.",
category: "Event",
likes: 4,
tags: ["news", "events"],
date: Date()
}
])
Theory/Concept: