$push and $pull
noun
say it: “DOL-er PUSH and DOL-er PULL” /ˈdɑlər ˈpʊʃ ənd ˈdɑlər ˈpʊl/
Two MongoDB update operators for arrays: $push adds an item to the end, and $pull removes every item equal to a value.
What do $push and $pull do in a MongoDB update?
$push adds an item to the end of an array field, even when the array already holds it, and $pull removes every item equal to the value you give. Both change the array in place, without rewriting the document.
db.recipes.updateOne({ _id: 4 }, { $push: { tags: "weeknight" } });
db.recipes.updateOne({ _id: 4 }, { $pull: { tags: "spicy" } });
db.recipes.findOne({ _id: 4 }).tags;
// [ 'vegan', 'weeknight' ]
In a sentence
- “$push and $pull change one array inside a document without sending the whole document back.”
- “The cookbook app uses $push and $pull to add tags to a recipe and take them off again.”
Level: Apprentice · More MongoDB terms at this level
Updated