midMongoDB
What is the difference between $push and $addToSet?
Updated May 1, 2026
Short answer
$push adds an element to an array; $addToSet adds it only if it doesn't already exist.
Deep explanation
$push allows duplicates and is faster. $addToSet ensures uniqueness but has a slight performance overhead due to the check.
Real-world example
Adding unique tags to a blog post.
Common mistakes
- Using $push when you need unique values, leading to duplicate data entry.
Follow-up questions
- How to push multiple items at once?