이 delete 명령은 컬렉션에서 문서를 제거하는 데 사용됩니다. 지정된 쿼리 필터에 따라 단일 문서 또는 여러 문서를 삭제할 수 있습니다.
문법
명령에 대한 delete 기본 구문은 다음과 같습니다.
db.collection.deleteOne(
<filter>,
<options>
)
db.collection.deleteMany(
<filter>,
<options>
)
매개 변수
| 매개 변수 | Description |
|---|---|
<filter> |
삭제 조건을 지정하는 문서입니다. 필터와 일치하는 문서만 삭제됩니다. |
options |
Optional. 삭제 작업에 대한 옵션을 지정하는 문서입니다. 일반적인 옵션에는 writeConcern 및 데이터 정렬이 포함됩니다. |
예제(들)
StoreData 데이터베이스의 Store 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
예제 1 - 컬렉션의 모든 문서 삭제
db.stores.deleteMany({})
예제 2 - 지정된 쿼리 필터와 일치하는 문서 삭제
db.stores.deleteOne({"_id": "68471088-4d45-4164-ae58-a9428d12f310"})
예제 3 - 지정된 쿼리 필터와 일치하는 모든 문서 삭제
db.stores.deleteMany({"promotionEvents.discounts.discountPercentage": 21}, {"limit": 0})
예제 3 - 지정된 쿼리 필터와 일치하는 여러 문서 중 하나만 삭제합니다.
db.stores.deleteMany({"promotionEvents.discounts.discountPercentage": 21}, {"limit": 1})