$push appends a value to an array field. If the field does not exist it creates a new array. Unlike $addToSet, $push allows duplicates.
Basic Usage
// Append a single tag db.articles.updateOne( { title: "MongoDB Tips" }, { $push: { tags: "database" } } ) // Append an embedded document db.users.updateOne( { _id: 1 }, { $push: { addresses: { city: "Mumbai", pin: "400001" } } } )
Push Multiple Values with $each
// Correct — $each pushes all three as separate elements db.articles.updateOne( { title: "MongoDB Tips" }, { $push: { tags: { $each: ["nosql", "backend", "cloud"] } } } ) // WRONG — pushes the entire array as ONE nested element db.articles.updateOne( { title: "MongoDB Tips" }, { $push: { tags: ["nosql", "backend"] } } ) // Before: tags: ["mongodb"] // After: tags: ["mongodb", ["nosql", "backend"]] ← nested array!
$push inserts it as a single nested element — not individual items. Always use $each when pushing multiple values as separate elements.Field Creation on First Push
// "tags" doesn't exist → $push creates it as a new array db.posts.updateOne({ _id: 1 }, { $push: { tags: "first" } }) // Before: { _id: 1, title: "Post" } // After: { _id: 1, title: "Post", tags: ["first"] }