Skip to content
Justice Tech School

Updating Arrays

$push adds an item, $addToSet adds if missing, $pull removes matches, $pop removes the first or last and the positional operator $ updates the matched element.

Example
db.posts.updateOne({ _id: 1 }, { $push: { tags: "new" } })
db.posts.updateOne({ _id: 1 }, { $addToSet: { tags: "db" } })
db.posts.updateOne({ _id: 1 }, { $pull: { tags: "old" } })

Exercise

Add without duplicates.

{ $: { tags: "db" } }

Quiz Test

Which avoids duplicates?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.