$pullAll - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$pullAll

The $pullAll operator in Amazon DocumentDB is used to remove all instances of the specified values from an array field. This is particularly useful when you need to remove multiple elements from an array in a single operation.

Parameters

  • field: The name of the array field from which to remove the elements.

  • value: An array of values to remove from the array field.

Example (MongoDB Shell)

The following example demonstrates how to use the $pullAll operator to remove multiple elements from an array field.

Create sample documents

db.restaurants.insert([ { "name": "Taj Mahal", "cuisine": "Indian", "features": ["Private Dining", "Live Music"] }, { "name": "Golden Palace", "cuisine": "Chinese", "features": ["Private Dining", "Takeout"] }, { "name": "Olive Garden", "cuisine": "Italian", "features": ["Private Dining", "Outdoor Seating"] } ])

Query example

db.restaurants.update( { "name": "Taj Mahal" }, { $pullAll: { "features": ["Private Dining", "Live Music"] } } )

Output

{ "name": "Taj Mahal", "cuisine": "Indian", "features": [] }

Code examples

To view a code example for using the $pullAll command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('restaurants'); await collection.updateMany( { "name": "Taj Mahal" }, { $pullAll: { "features": ["Private Dining", "Live Music"] } } ); const updatedDocument = await collection.findOne({ "name": "Taj Mahal" }); console.log(updatedDocument); await client.close(); } main();
Python
from pymongo import MongoClient def main(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['restaurants'] collection.update_many( {"name": "Taj Mahal"}, {"$pullAll": {"features": ["Private Dining", "Live Music"]}} ) updated_document = collection.find_one({"name": "Taj Mahal"}) print(updated_document) client.close() if __name__ == '__main__': main()