Docs Menu

๋ฌธ์„œ ์‚ญ์ œ

์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” MongoDB Java ๋“œ๋ผ์ด๋ฒ„๋กœ ๋ฌธ์„œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฟผ๋ฆฌ ํ•„ํ„ฐ๋ฅผ deleteOne(), deleteMany() ๋˜๋Š” findOneAndDelete() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

deleteOne() ๋ฉ”์„œ๋“œ๋Š” ๋‹จ์ผ ๋ฌธ์„œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ ํ•„ํ„ฐ๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฌธ์„œ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ ๋ฉ”์„œ๋“œ๋Š” collection์—์„œ ์ฒ˜์Œ ์ผ์น˜ํ•˜๋Š” ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

deleteMany() ๋ฉ”์„œ๋“œ๋Š” ์ฟผ๋ฆฌ ํ•„ํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

findOneAndDelete() ๋ฉ”์„œ๋“œ๋Š” collection์—์„œ ์ฒ˜์Œ ์ผ์น˜ํ•˜๋Š” ํ•ญ๋ชฉ์„ ์›์ž ๋‹จ์œ„๋กœ ์ฐพ์•„์„œ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

์ธ๋ฑ์Šค๋ฅผ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ๋˜๋Š” ํžŒํŠธ๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด deleteOne() ๋ฐ deleteMany() ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ DeleteOptions ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, ์ธ๋ฑ์Šค์— ํžŒํŠธ๋ฅผ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜, ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, ๋ฐ˜ํ™˜๋œ ๋ฌธ์„œ์— ํ”„๋กœ์ ์…˜์„ ์ง€์ •ํ•˜๋ ค๋ฉด findOneAndDelete() ๋ฉ”์„œ๋“œ์˜ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ FindOneAndDeleteOptions ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ๋Š” 8๊ฐ€์ง€ ์ƒ‰์ƒ์˜ ํŽ˜์ธํŠธ๋ฅผ ํŒ๋งคํ•˜๋Š” ํŽ˜์ธํŠธ ๊ฐ€๊ฒŒ์— ๊ด€ํ•œ ์˜ˆ์ž…๋‹ˆ๋‹ค. ์ด ์Šคํ† ์–ด๋Š” ์—ฐ๋ก€ ์˜จ๋ผ์ธ ํŒ๋งค๋ฅผ ํ†ตํ•ด paint_inventory collection์— ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ํฌํ•จ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 3, "color": "blue", "qty": 0 }
{ "_id": 4, "color": "white", "qty": 0 }
{ "_id": 5, "color": "yellow", "qty": 6 }
{ "_id": 6, "color": "pink", "qty": 0 }
{ "_id": 7, "color": "green", "qty": 0 }
{ "_id": 8, "color": "black", "qty": 8 }

ํŽ˜์ธํŠธ ๋งค์žฅ ์›น์‚ฌ์ดํŠธ์—๋Š” paint_inventory collection์˜ ๋ชจ๋“  ๋ฌธ์„œ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋งค์žฅ์—์„œ๋Š” ๊ณ ๊ฐ์˜ ํ˜ผ๋™์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์žฌ๊ณ ๊ฐ€ ์—†๋Š” ์ƒ‰์ƒ์„ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์žฌ๊ณ ๊ฐ€ ์—†๋Š” ์ƒ‰์ƒ์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด qty ์ด 0 ์ธ paint_inventory ์ปฌ๋ ‰์…˜์„ ์ฟผ๋ฆฌํ•˜๊ณ  ์ฟผ๋ฆฌ๋ฅผ deleteMany() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

Bson filter = Filters.eq("qty", 0);
collection.deleteMany(filter);

๋‹ค์Œ์€ paint_inventory collection์— ๋‚จ์•„ ์žˆ๋Š” ๋ฌธ์„œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 5, "color": "yellow", "qty": 6 }
{ "_id": 8, "color": "black", "qty": 8 }

์ด ๊ฐ€๊ฒŒ๋Š” ๋‚จ์€ ๋…ธ๋ž€์ƒ‰ ํŽ˜์ธํŠธ ์ˆ˜๋Ÿ‰์„ ๊ธฐ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋…ธ๋ž€์ƒ‰์˜ qty ๋Š” ์ด์ œ 0 ์ด๋ฉฐ collection์—์„œ ๋…ธ๋ž€์ƒ‰์„ ์ œ๊ฑฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋…ธ๋ž€์ƒ‰์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด color๊ฐ€ "yellow"์ธ paint_inventory ์ปฌ๋ ‰์…˜์„ ์ฟผ๋ฆฌํ•˜๊ณ  deleteOne() ๋ฉ”์„œ๋“œ์— ์ฟผ๋ฆฌ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

Bson filter = Filters.eq("color", "yellow");
collection.deleteOne(filter);

๋‹ค์Œ์€ paint_inventory collection์— ๋‚จ์•„ ์žˆ๋Š” ๋ฌธ์„œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 8, "color": "black", "qty": 8 }

์ƒ์ ์—์„œ๋Š” ๋‚˜๋จธ์ง€ ์ˆ˜๋Ÿ‰์˜ ์ž์ฃผ์ƒ‰ ํŽ˜์ธํŠธ๋ฅผ ๋ฝ‘๊ณ  paint_inventory collection์—์„œ ์ž์ฃผ์ƒ‰์„ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ƒ‰์ƒ์„ ์„ ํƒํ•˜๋ ค๋ฉด color ์ด "purple" ์ธ paint_inventory collection์„ ์ฟผ๋ฆฌํ•˜๊ณ  ์ฟผ๋ฆฌ๋ฅผ findOneAndDelete() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

Bson filter = Filters.eq("color", "purple");
System.out.println(collection.findOneAndDelete(filter).toJson());

๋‹ค๋ฅธ ์‚ญ์ œ ๋ฉ”์„œ๋“œ์™€ ๋‹ฌ๋ฆฌ findOneAndDelete() ์€ ์‚ญ์ œ๋œ ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ "_id": 2, "color": "purple", "qty": 8 }

์ฐธ๊ณ 

์ฟผ๋ฆฌ ํ•„ํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์—†์œผ๋ฉด ๋ฌธ์„œ๊ฐ€ ์‚ญ์ œ๋˜์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ๋Š” null ์„(๋ฅผ) ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ paint_inventory collection์— ๋‚จ์•„ ์žˆ๋Š” ๋ฌธ์„œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 8, "color": "black", "qty": 8 }

์ฐธ๊ณ 

์„ค์ • ์˜ˆ์‹œ

์ด ์˜ˆ์‹œ ์—ฐ๊ฒฐ URI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MongoDB ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. MongoDB ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ํ•™์Šต MongoClient ๋งŒ๋“ค๊ธฐ ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ด ์˜ˆ์‹œ Atlas ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ํฌํ•จ๋œ sample_mflix ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ movies ์ปฌ๋ ‰์…˜๋„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Atlas ์‹œ์ž‘ํ•˜๊ธฐ ๊ฐ€์ด๋“œ์— ๋”ฐ๋ผ MongoDB Atlas์˜ ๋ฌด๋ฃŒ ๊ณ„์ธต์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋Š” ํ•˜๋‚˜์˜ ์‚ญ์ œ ์ž‘์—…๊ณผ ๋‹ค์ˆ˜ ์‚ญ์ œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์™„์ „ํ•œ ๋…๋ฆฝํ˜• ํŒŒ์ผ ์ž…๋‹ˆ๋‹ค.

// Deletes documents from a collection by using the Java driver
package org.example;
import static com.mongodb.client.model.Filters.eq;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Filters.lt;
public class Delete {
public static void main(String[] args) {
// Replace the uri string with your MongoDB deployment's connection string
String uri = "<connection string uri>";
try (MongoClient mongoClient = MongoClients.create(uri)) {
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");
Bson deleteOneQuery = eq("title", "The Garbage Pail Kids Movie");
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
DeleteResult result = collection.deleteOne(deleteOneQuery);
System.out.println("Deleted document count - delete one: " + result.getDeletedCount());
Bson deleteManyQuery = lt("imdb.rating", 1.9);
// Deletes all documents that have an "imdb.rating" value less than 1.9
result = collection.deleteMany(deleteManyQuery);
// Prints the number of deleted documents
System.out.println("Deleted document count - delete many: " + result.getDeletedCount());
}
}
}
Deleted document count - query for one: 1
Deleted document count - unlimited query: 4

์ด ์˜ˆ์ œ์˜ ์ฟผ๋ฆฌ๋Š” eq() ๋ฐ lt() ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ์ฟผ๋ฆฌ . ํ•„ํ„ฐ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ํ•„ํ„ฐ ํด๋ž˜์Šค API ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋ฌธ์„œ๋ฅผ ์‚ญ์ œ ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ ๋ฐ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ API ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.