๋ฌธ์ ์ญ์
์ด ํ์ด์ง์ ๋ด์ฉ
๊ฐ์
์ด ๊ฐ์ด๋์์๋ 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 ๋ฌธ์
๋ฌธ์๋ฅผ ์ญ์ ๋ฐ ์ฌ์ฉ๋๋ ๋ฉ์๋ ๋ฐ ํด๋์ค์ ๋ํ ์์ธํ ๋ด์ฉ์ ๋ค์ API ์ค๋ช ์๋ฅผ ์ฐธ์กฐํ์ธ์.