$push modifiers
Reference group: Update operator families
What it does
Appends values and optionally sorts, positions, or caps the array.
Syntax
{$push: { arr: { $each, $sort?, $slice?, $position? } }}
Use case
Maintain bounded recent items.
$push modifiers: practical example
db.users.updateOne({ _id: userId }, { $push: { recent: { $each: [entry], $sort: { at: -1 }, $slice: 10 } } })💡 Explanation
Appends values and optionally sorts, positions, or caps the array.
When to use it
Maintain bounded recent items.
Key Takeaways
- Appends values and optionally sorts, positions, or caps the array.
- Typical use: Maintain bounded recent items.