$pullAll - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$pullAll

$pullAllOperator di Amazon DocumentDB digunakan untuk menghapus semua instance nilai yang ditentukan dari bidang array. Ini sangat berguna ketika Anda perlu menghapus beberapa elemen dari array dalam satu operasi.

Parameter

  • field: Nama bidang array dari mana untuk menghapus elemen.

  • value: Sebuah array nilai untuk menghapus dari bidang array.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan bagaimana menggunakan $pullAll operator untuk menghapus beberapa elemen dari bidang array.

Buat dokumen sampel

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"] } ])

Contoh kueri

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

Keluaran

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

Contoh kode

Untuk melihat contoh kode untuk menggunakan $pullAll perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

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()