$push and $pull — MongoDB Technical Interview Questions & Answers

Adding to or removing from an array field.

$push and $pull

  • Simple meaning: $push adds an item to an array. $pull removes items that match.
  • How to use: db.students.updateOne({ roll: 12 }, { $push: { skills: "MongoDB" } }). $pull: { $pull: { skills: "SQL" } }.
  • Try this: skills: [] then $push "Java" then $push "MongoDB".
  • Common mistake: $push a whole array when you wanted $each. Unbounded arrays (millions of events) — use another collection.
  • Interview tip: Arrays for small lists. $push / $pull / $addToSet. Mention document size.

Technical Interview Tip: Small skills array: $push. Big history: new collection.

Code Examples for $push and $pull

$push and $pull — working example

// Add a skill
db.users.updateOne(
  { email: "ada@example.com" },
  { $push: { skills: "Spring" } }
)

// Remove a skill
db.users.updateOne(
  { email: "ada@example.com" },
  { $pull: { skills: "Java" } }
)

$push and $pull — $push appends to an array; $pull removes matching values from an array.

How to Explain $push and $pull to an Interviewer

How do you add to an array field?

updateOne with { $push: { skills: "MongoDB" } }