翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$pullAll
Amazon DocumentDB の $pullAll演算子は、指定された値のすべてのインスタンスを配列フィールドから削除するために使用されます。これは、1 回のオペレーションで配列から複数の要素を削除する必要がある場合に特に便利です。
パラメータ
例 (MongoDB シェル)
次の例は、 $pullAll演算子を使用して配列フィールドから複数の要素を削除する方法を示しています。
サンプルドキュメントを作成する
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"]
}
])
クエリの例
db.restaurants.update(
{ "name": "Taj Mahal" },
{ $pullAll: { "features": ["Private Dining", "Live Music"] } }
)
出力
{
"name": "Taj Mahal",
"cuisine": "Indian",
"features": []
}
コードの例
$pullAll コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。
- 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()