# How to push or pull item from an array in Mongoose

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jul 10, 2022. Topics: Node.js, Mongoose, MongoDB.
Canonical URL: https://attacomsian.com/blog/mongoose-push-pull-items-from-array

> A quick guide to learn about $push and $pull operators in Mongoose to push and pull items from an array field.

There are two ways to push an item to an array in Mongoose. The simplest way is to [retrieve the document](https://attacomsian.com/blog/mongoose-find-documents) from MongoDB, [push](https://attacomsian.com/blog/javascript-array-add-items) new items into the array, and then call the `save()` method to [update the document](https://attacomsian.com/blog/mongoose-update-documents).

Let us say that you have got the following `Student` model:

```javascript
const mongoose = require('mongoose')
const { Schema } = mongoose

const Student = mongoose.model(
  'Student',
  new Schema({
    name: String,
    friends: [String]
  })
)
```

The following example demonstrates how you can append new items into an array in Mongoose:

```javascript
// Create new document
await Student.create({ _id: 1, name: 'John Doe', friends: ['Alex'] })

// Retrieve document
const doc = await Student.findOne({ _id: 1 })

// Append items to `friends`
doc.friends.push('Maria')

// Update document
await doc.save()
```

The second method to append an item into an array is using the `$push` operator. The `$push` operator appends specified items into an array without loading them first into memory as shown below:

```javascript
await Student.updateOne({ _id: 1 }, { $push: { friends: 'Maria' } })
```

To append multiple items at once, use `$push` with the `$each` modifier:

```javascript
await Student.updateOne({ _id: 1 }, { $push: { friends: { $each: ['Maria', 'Jovan'] } } })
```

Similarly, to remove items from an array, MongoDB provides the `$pull` operator. It removes from an existing array all instances of a value or values that match a specified condition:

```javascript
// Remove single item
await Student.updateOne({ _id: 1 }, { $pull: { friends: 'Alex' } })

// Remove multiple items with `$in` modifier
await Student.updateOne({ _id: 1 }, { $pull: { friends: { $in: ['Maria', 'Jovan'] } } })
```
