Docs Menu

๋Œ€๋Ÿ‰ ์ž‘์—…

์ด ๊ฐ€์ด๋“œ ์—์„œ๋Š” Rust ์šด์ „์ž ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€๋Ÿ‰ ์ž‘์—… ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ€๋Ÿ‰ ์ž‘์—…์€ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ์“ฐ๊ธฐ (write) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋Š” <database>.<collection> ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„๊ณผ ์ปฌ๋ ‰์…˜ ์ด๋ฆ„์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค. Client ์ธ์Šคํ„ด์Šค ์—์„œ ๋Œ€๋Ÿ‰ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ํด๋ผ์ด์–ธํŠธ ๊ฐ€ ์•ก์„ธ์Šคํ•˜๋Š” ํด๋Ÿฌ์Šคํ„ฐ ์˜ ๋ชจ๋“  ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์— ๋Œ€ํ•ด ๋Œ€๋Ÿ‰ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ€๋Ÿ‰ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์„œ๋ฒ„ ์— ๋Œ€ํ•œ ํ˜ธ์ถœ ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋Ÿ‰ ์ž‘์—…์€ ๊ฐ ์ž‘์—…์— ๋Œ€ํ•œ ์š”์ฒญ ์„ ๋ณด๋‚ด๋Š” ๋Œ€์‹  ํ•˜๋‚˜์˜ ์กฐ์น˜ ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ์—๋Š” ๋‹ค์Œ ์„น์…˜์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ๋Š” ๋Œ€๋Ÿ‰ ์ž‘์—… ์˜ˆ์ œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

  • ๋Œ€๋Ÿ‰ ์ž‘์—… ์œ ํ˜• WriteModel ์œ ํ˜•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€๋Ÿ‰ ์‚ฝ์ž…, ๋ฐ”๊พธ๊ธฐ, ์—…๋ฐ์ดํŠธ ๋ฐ ์‚ญ์ œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐ˜ํ™˜ ์œ ํ˜• ์€ bulk_write() ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ ๊ฐ’๊ณผ ๋Œ€๋Ÿ‰ ์ž‘์—…์— ๋Œ€ํ•œ ์ •๋ณด์— ์•ก์„ธ์Šค ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

  • ๋™์ž‘ ์ˆ˜์ •์€ bulk_write() ๋ฉ”์„œ๋“œ์˜ ๊ธฐ๋ณธ๊ฐ’ ๋™์ž‘์„ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

  • ํ˜ผํ•ฉ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์“ฐ๊ธฐ ์—์„œ๋Š” ํ•œ ๋ฒˆ์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋กœ ์—ฌ๋Ÿฌ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ๋Œ€ํ•ด ๋Œ€๋Ÿ‰ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

  • ์ถ”๊ฐ€ ์ •๋ณด์—์„œ ์ด ๊ฐ€์ด๋“œ์— ์–ธ๊ธ‰๋œ ์œ ํ˜• ๋ฐ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ๋ฆฌ์†Œ์Šค ๋ฐ API ๋ฌธ์„œ ๋งํฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ค‘์š”

๋Œ€๋Ÿ‰ ์“ฐ๊ธฐ (write) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด ๋‹ค์Œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.

  • MongoDB Server ๋ฒ„์ „ 8.0 ์ด์ƒ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • Rust ์šด์ „์ž ๋ฒ„์ „ 3.0 ์ด์ƒ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ ์˜ ์˜ˆ์ œ์—์„œ๋Š” db ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์˜ mushrooms ์ปฌ๋ ‰์…˜ ์— ์ €์žฅ๋œ ๋‹ค์Œ ์ƒ˜ํ”Œ ๋ฌธ์„œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

let docs = vec![
doc! {"name" : "portobello", "color" : "brown", "edible" : true },
doc! {"name" : "chanterelle", "color" : "yellow", "edible" : true },
doc! {"name" : "oyster", "color" : "white", "edible" : true },
doc! {"name" : "fly agaric", "color" : "red", "edible" : false },
];

์‚ฌ์šฉ์ž ์ง€์ • ๊ตฌ์กฐ์ฒด ์œ ํ˜•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Mushroom ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋ธ๋งํ•˜๋Š” ๋Œ€๋Ÿ‰ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ˆ์‹œ ๋Š” ์ด ํŽ˜์ด์ง€์˜ ๊ตฌ์กฐ์ฒด ์‚ฝ์ž… ์˜ˆ์ œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋Œ€๋Ÿ‰ ์“ฐ๊ธฐ (write) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด WriteModel ์—ด๊ฑฐํ˜• ์ธ์Šคํ„ด์Šค์˜ ๋ฐฐ์—ด ์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์ด ์„น์…˜์—์„œ๋Š” ํ•ด๋‹น WriteModel ์œ ํ˜•์„ ์ •์˜ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋Œ€๋Ÿ‰ ์“ฐ๊ธฐ (write) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ํ•™์Šต ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŒ

๋‹จ์ผ bulk_write() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋กœ ์—ฌ๋Ÿฌ ์œ ํ˜•์˜ ์“ฐ๊ธฐ (write) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. UpdateOneModel ๋ฐ InsertOneModel ๋ฅผ ๋™์ผํ•œ bulk_write() ํ˜ธ์ถœ์— ์ „๋‹ฌํ•˜๋Š” ์˜ˆ์‹œ ๋ฅผ ๋ณด๋ ค๋ฉด ์ด ๊ฐ€์ด๋“œ ์˜ ๋™์ž‘ ์ˆ˜์ • ์˜ˆ์‹œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋Œ€๋Ÿ‰ ์‚ฝ์ž… ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ์‚ฝ์ž…ํ•˜๋ ค๋Š” ๊ฐ ๋ฌธ์„œ ์— ๋Œ€ํ•ด InsertOneModel ์ธ์Šคํ„ด์Šค ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ชจ๋ธ ๋ชฉ๋ก์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ํ‘œ์—์„œ๋Š” ํ•ด๋‹น ๋นŒ๋” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ค์ •ํ•˜๋‹ค ์ˆ˜ ์žˆ๋Š” InsertOneModel ํ•„๋“œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

ํ•„๋“œ
์„ค๋ช…

namespace

The namespace on which the insert is performed.
Type: Namespace

document

The document to insert.
Type: Document

์ด ์˜ˆ์—์„œ๋Š” ๋‹ค์Œ ์กฐ์น˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐฐ์—ด ์— InsertOneModel ์ธ์Šคํ„ด์Šค ๋‘ ๊ฐœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ InsertOneModel ๋Š” db.mushrooms ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์— ์‚ฝ์ž…ํ•  ๋ฌธ์„œ ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

  • ๋ชจ๋ธ ๋ฐฐ์—ด ์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

  • ์‚ฝ์ž…๋œ ๋ฌธ์„œ ์ˆ˜๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

let mushrooms: Collection<Document> = client.database("db").collection("mushrooms");
let models = vec![
InsertOneModel::builder()
.namespace(mushrooms.namespace())
.document(doc! {
"name": "lion's mane",
"color": "white",
"edible": true
})
.build(),
InsertOneModel::builder()
.namespace(mushrooms.namespace())
.document(doc! {
"name": "angel wing",
"color": "white",
"edible": false
})
.build(),
];
let result = client.bulk_write(models).await?;
println!("Inserted documents: {}", result.inserted_count);
Inserted documents: 2

๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ตฌ์กฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ db.mushrooms ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์— ๋Œ€๋Ÿ‰ ์‚ฝ์ž…ํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ๋ชจ๋ธ๋งํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์˜ˆ์‹œ ์—์„œ๋Š” ์•ž์˜ ๋ฌธ์„œ ์‚ฝ์ž… ์˜ˆ์ œ ์™€ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ ๋‹ค์Œ Mushroom ๊ตฌ์กฐ์ฒด ์œ ํ˜•์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

#[derive(Serialize)]
struct Mushroom {
name: String,
color: String,
edible: bool,
}

๋‹ค์Œ ์ฝ”๋“œ๋Š” insert_one_model() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ Mushroom ์ธ์Šคํ„ด์Šค ์—์„œ InsertOneModel ๋ฅผ ๊ตฌ์„ฑํ•œ ๋‹ค์Œ ๋‘ ๋ชจ๋ธ์„ ๋Œ€๋Ÿ‰ ์ž‘์—…์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

let mushrooms: Collection<Mushroom> = client.database("db").collection("mushrooms");
let lions_mane = Mushroom {
name: "lion's mane".to_string(),
color: "white".to_string(),
edible: true,
};
let angel_wing = Mushroom {
name: "angel wing".to_string(),
color: "white".to_string(),
edible: false,
};
let lions_mane_model = mushrooms.insert_one_model(lions_mane)?;
let angel_wing_model = mushrooms.insert_one_model(angel_wing)?;
let result = client.bulk_write([lions_mane_model, angel_wing_model]).await?;
println!("Inserted documents: {}", result.inserted_count);
Inserted documents: 2

ํŒ

Rust ์šด์ „์ž ์˜ ์‚ฌ์šฉ์ž ์ง€์ • ๊ตฌ์กฐ์ฒด ์œ ํ˜• ๋ฐ ์ง๋ ฌํ™”์— ํ•™์Šต ๋ณด๋ ค๋ฉด ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ๋ฐ ์ง๋ ฌํ™”๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ผ๊ด„ ๊ต์ฒด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๊ต์ฒดํ•˜๋ ค๋Š” ๊ฐ ๋ฌธ์„œ ์— ๋Œ€ํ•ด ReplaceOneModel ์ธ์Šคํ„ด์Šค ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ชจ๋ธ ๋ชฉ๋ก์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ํ‘œ์—์„œ๋Š” ํ•ด๋‹น ๋นŒ๋” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ค์ •ํ•˜๋‹ค ์ˆ˜ ์žˆ๋Š” ReplaceOneModel ํ•„๋“œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

ํ•„๋“œ
์„ค๋ช…

namespace

The namespace on which the operation is performed.
Type: Namespace

filter

The filter that matches the document you want to replace.
Type: Document

replacement

The replacement document.
Type: Document

collation

(Optional) The collation to use when sorting results. To learn more about collations, see the Collations guide.
Type: Document

hint

(Optional) The index to use for the operation. To learn more about indexes, see the Indexes guide.
Type: Bson

upsert

(Optional) Whether a new document is created if no document matches the filter.
By default, this field is set to false.
Type: bool

์ด ์˜ˆ์—์„œ๋Š” ๋‹ค์Œ ์กฐ์น˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐฐ์—ด ์— ReplaceOneModel ์ธ์Šคํ„ด์Šค ๋‘ ๊ฐœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ReplaceOneModel ์ธ์Šคํ„ด์Šค์—๋Š” db.mushrooms ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์—์„œ ๋ฒ„์„ฏ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์„œ๋ฅผ ๊ต์ฒดํ•˜๋Š” ์ง€์นจ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ชจ๋ธ ๋ฐฐ์—ด ์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

  • ์ˆ˜์ •๋œ ๋ฌธ์„œ ์ˆ˜๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

let mushrooms: Collection<Document> = client.database("db").collection("mushrooms");
let models = vec![
ReplaceOneModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "name": "portobello" })
.replacement(doc! {
"name": "cremini",
"color": "brown",
"edible": true
})
.build(),
ReplaceOneModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "name": "oyster" })
.replacement(doc! {
"name": "golden oyster",
"color": "yellow",
"edible": true
})
.upsert(true)
.build(),
];
let result = client.bulk_write(models).await?;
println!("Modified documents: {}", result.modified_count);
Modified documents: 2

์ผ๊ด„ ์—…๋ฐ์ดํŠธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ์—…๋ฐ์ดํŠธํ•˜๋ ค๋Š” ๊ฐ ์—…๋ฐ์ดํŠธ ์— ๋Œ€ํ•ด UpdateOneModel ๋˜๋Š” UpdateManyModel ์ธ์Šคํ„ด์Šค ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ชจ๋ธ ๋ชฉ๋ก์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. UpdateOneModel ์€ ํ•„ํ„ฐํ•˜๋‹ค ์™€ ์ผ์น˜ํ•˜๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์„œ ๋งŒ ์—…๋ฐ์ดํŠธํ•˜๊ณ  UpdateManyModel ๋Š” ํ•„ํ„ฐํ•˜๋‹ค ์™€ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ํ‘œ์—์„œ๋Š” ํ•ด๋‹น ๋นŒ๋” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ค์ •ํ•˜๋‹ค ์ˆ˜ ์žˆ๋Š” UpdateOneModel ๋ฐ UpdateManyModel ํ•„๋“œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

ํ•„๋“œ
์„ค๋ช…

namespace

The namespace on which the operation is performed.
Type: Namespace

filter

The filter that matches one or more documents you want to update. When specified in an UpdateOneModel, only the first matching document will be updated. When specified in an UpdateManyModel, all matching documents will be updated.
Type: Document

update

The update to perform.
Type: UpdateModifications

array_filters

(Optional) A set of filters specifying which array elements an update applies to if you are updating an array-valued field.
Type: Array

collation

(Optional) The collation to use when sorting results. To learn more about collations, see the Collations guide.
Type: Document

hint

(Optional) The index to use for the operation. To learn more about indexes, see the Indexes guide.
Type: Bson

upsert

(Optional) Whether a new document is created if no document matches the filter. By default, this field is set to false.
Type: bool

์ด ์˜ˆ์—์„œ๋Š” ๋‹ค์Œ ์กฐ์น˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐฐ์—ด ์— UpdateOneModel ๋ฐ UpdateManyModel ์ธ์Šคํ„ด์Šค ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์—๋Š” db.mushrooms ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์—์„œ ๋ฒ„์„ฏ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์„œ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์œ„ํ•œ ์ง€์นจ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ชจ๋ธ ๋ฐฐ์—ด ์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

  • ์ˆ˜์ •๋œ ๋ฌธ์„œ ์ˆ˜๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

let mushrooms: Collection<Document> = client.database("db").collection("mushrooms");
let models = vec![
WriteModel::UpdateOne(
UpdateOneModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "name": "fly agaric" })
.update(doc! { "$set": { "name": "fly amanita" } })
.upsert(true)
.build(),
),
WriteModel::UpdateMany(
UpdateManyModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "color": "yellow" })
.update(doc! { "$set": { "color": "yellow/orange" } })
.build(),
),
];
let result = client.bulk_write(models).await?;
println!("Modified documents: {}", result.modified_count);
Modified documents: 2

์ผ๊ด„ ์‚ญ์ œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๊ฐ ์‚ญ์ œ ์ž‘์—…์— ๋Œ€ํ•ด DeleteOneModel ๋˜๋Š” DeleteManyModel ์ธ์Šคํ„ด์Šค ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ชจ๋ธ ๋ชฉ๋ก์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. DeleteOneModel ์€ ํ•„ํ„ฐํ•˜๋‹ค ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ ๋ฅผ ํ•˜๋‚˜๋งŒ ์‚ญ์ œํ•˜๊ณ  DeleteManyModel ๋Š” ํ•„ํ„ฐํ•˜๋‹ค ์™€ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ํ‘œ์—์„œ๋Š” ํ•ด๋‹น ๋นŒ๋” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ค์ •ํ•˜๋‹ค ์ˆ˜ ์žˆ๋Š” DeleteOneModel ๋ฐ DeleteManyModel ํ•„๋“œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

ํ•„๋“œ
์„ค๋ช…

namespace

The namespace on which the operation is performed.
Type: Namespace

filter

The filter that matches one or more documents you want to delete. When specified in a DeleteOneModel, only the first matching document will be deleted. When specified in a DeleteManyModel, all matching documents will be deleted.
Type: Document

collation

(Optional) The collation to use when sorting results. To learn more about collations, see the Collations guide.
Type: Document

hint

(Optional) The index to use for the operation. To learn more about indexes, see the Indexes guide.
Type: Bson

์ด ์˜ˆ์—์„œ๋Š” ๋‹ค์Œ ์กฐ์น˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐฐ์—ด ์— DeleteOneModel ๋ฐ DeleteManyModel ์ธ์Šคํ„ด์Šค ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์—๋Š” db.mushrooms ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์—์„œ ๋ฒ„์„ฏ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์„œ๋ฅผ ์‚ญ์ œ ํ•˜๋Š” ์ง€์นจ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ชจ๋ธ ๋ฐฐ์—ด ์„ bulk_write() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

  • ์‚ญ์ œ๋œ ๋ฌธ์„œ ์ˆ˜๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

let mushrooms: Collection<Document> = client.database("db").collection("mushrooms");
let models = vec![
WriteModel::DeleteOne(
DeleteOneModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "color": "red" })
.build(),
),
WriteModel::DeleteMany(
DeleteManyModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "edible": true })
.build(),
),
];
let result = client.bulk_write(models).await?;
println!("Deleted documents: {}", result.deleted_count);
Deleted documents: 4

bulk_write() ๋ฉ”์„œ๋“œ๋Š” ๋Œ€๋Ÿ‰ ์ž‘์—…์— ๋Œ€ํ•œ ์ •๋ณด์— ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ๋Š” SummaryBulkWriteResult ๊ตฌ์กฐ์ฒด ์ธ์Šคํ„ด์Šค ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

SummaryBulkWriteResult ์œ ํ˜•์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•„๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • inserted_count: ์‚ฝ์ž…๋œ ๋ฌธ์„œ ์ˆ˜

  • matched_count: ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ ์ˆ˜

  • modified_count: ์—…๋ฐ์ดํŠธ๋œ ๋ฌธ์„œ ์ˆ˜

  • upserted_count: ์—…์„œํŠธ๋œ ๋ฌธ์„œ ์ˆ˜

  • deleted_count: ์‚ญ์ œ๋œ ๋ฌธ์„œ ์ˆ˜

verbose_results() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์ž‘์—…์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. verbose_results() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ ํ•„๋“œ๊ฐ€ ์žˆ๋Š” VerboseBulkWriteResult ๊ตฌ์กฐ์ฒด ์ธ์Šคํ„ด์Šค ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • delete_results: ์„ฑ๊ณต์ ์ธ ๊ฐ ์‚ญ์ œ ์ž‘์—…์˜ ๊ฒฐ๊ณผ

  • insert_results: ์„ฑ๊ณต์ ์ธ ๊ฐ ์‚ฝ์ž… ์ž‘์—…์˜ ๊ฒฐ๊ณผ

  • update_results: ์„ฑ๊ณต์ ์ธ ๊ฐ ์—…๋ฐ์ดํŠธ ์ž‘์—…์˜ ๊ฒฐ๊ณผ

  • summary: ๊ฐ ์ž‘์—… ์œ ํ˜•์˜ ๊ฒฐ๊ณผ ์š”์•ฝ

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” verbose_results() ๋ฉ”์„œ๋“œ๋ฅผ bulk_write() ๋ฉ”์„œ๋“œ์— ์—ฐ๊ฒฐํ•˜๊ณ  ์—…๋ฐ์ดํŠธ ๋ฐ ์‚ญ์ œ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

let models = vec![
WriteModel::DeleteOne(
DeleteOneModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "name": "oyster" })
.build(),
),
WriteModel::UpdateOne(
UpdateOneModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "name": "chanterelle" })
.update(doc! { "$set": { "season": ["July", "August", "September"] } })
.build(),
),
];
let result = client.bulk_write(models).verbose_results().await?;
println!(
"Update results: {:?}\nDelete results: {:?}\n",
result.update_results, result.delete_results
);
Update results: {1: UpdateResult { matched_count: 1, modified_count: 1, upserted_id: None }}
Delete results: {0: DeleteResult { deleted_count: 1 }}

BulkWriteOptions ํ•„๋“œ ๊ฐ’์„ ์„ค์ •ํ•˜์—ฌ bulk_write() ๋ฉ”์„œ๋“œ์˜ ๋™์ž‘์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ์ฒด ํ•„๋“œ๋ฅผ ์„ค์ •ํ•˜๋‹ค ํ•˜๋ ค๋ฉด ํ•„๋“œ์˜ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ bulk_write() ๋ฉ”์„œ๋“œ์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

BulkWriteOptions ๊ตฌ์กฐ์ฒด์—๋Š” ๋‹ค์Œ ํ•„๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•„๋“œ
์„ค๋ช…
๊ธฐ๋ณธ๊ฐ’

ordered

Whether the operations run in the order in which they were specified.
When set to true, one failed operation prevents subsequent operations from running.
When set to false, the server continues to attempt write operations if one fails.
Type: bool

true

bypass_document_validation

Whether document-level validation is bypassed.
Type: bool

false

comment

An arbitrary comment to help trace the operation through the database profiler, currentOp, and logs.
Type: Bson

None

let_vars

A map of parameter names and values to apply to all operations within the bulk write. Values must be constant or closed expressions that do not reference document fields.
Type: Document

None

write_concern

The write concern to use for this bulk operation.
Type: WriteConcern

๋„ค์ž„์ŠคํŽ˜์ด์Šค์˜ ์“ฐ๊ธฐ ๊ณ ๋ ค (write concern) ๊ณ ๋ ค๋ฅผ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์‹œ ์—์„œ๋Š” mushrooms ์ปฌ๋ ‰์…˜ ์—์„œ ์—…๋ฐ์ดํŠธ ๋ฐ ์‚ฝ์ž… ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ordered() ๋ฉ”์„œ๋“œ๋ฅผ bulk_write() ๋ฉ”์„œ๋“œ์— ์—ฐ๊ฒฐํ•˜์—ฌ ordered ํ•„๋“œ ๋ฅผ false (์œผ)๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

let mushrooms: Collection<Document> = client.database("db").collection("mushrooms");
let models = vec![
WriteModel::UpdateOne(UpdateOneModel::builder()
.namespace(mushrooms.namespace())
.filter(doc! { "name": "portobello" })
.update(doc! { "$set": { "_id": 123 } })
.upsert(true)
.build()),
WriteModel::InsertOne(InsertOneModel::builder()
.namespace(mushrooms.namespace())
.document(doc! {
"name": "reishi",
"color": "red/brown",
"edible": true
})
.build()),
];
let result = client.bulk_write(models).ordered(false).await?;
println!(
"Inserted documents: {}\nDeleted documents: {}",
result.inserted_count, result.deleted_count
);
Error: Error { kind: BulkWrite(BulkWriteError { write_concern_errors: [], write_errors:
{0: WriteError { code: 66, code_name: None, message: "Plan executor error during update ::
caused by :: Performing an update on the path '_id' would modify the immutable field '_id'",
details: None }}, partial_result: Some(Summary(SummaryBulkWriteResult { inserted_count: 1,
matched_count: 0, modified_count: 0, upserted_count: 0, deleted_count: 0 })) }),
labels: ... }

_id ํ•„๋“œ ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์—…๋ฐ์ดํŠธ ์ž‘์—…์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. UpdateOneModel ์—๋Š” ์ด ํ•„๋“œ ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์œ„ํ•œ ์ง€์นจ์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋Œ€๋Ÿ‰ ์ž‘์—…์€ BulkWriteError ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‚ฝ์ž… ์ž‘์—…๋งŒ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ordered ํ•„๋“œ ๋ฅผ true ๋กœ ์„ค์ •ํ•˜๋‹ค ํ•˜๋ฉด ์šด์ „์ž ๋Š” ์—…๋ฐ์ดํŠธ ์ž‘์—…์ด ์‹คํŒจํ•œ ํ›„ ํ›„์† ์ž‘์—…์„ ์‹œ๋„ํ•˜์ง€ ์•Š์œผ๋ฉฐ ์šด์ „์ž ๋Š” ๋ฌธ์„œ๋ฅผ ์‚ฝ์ž…ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€์˜ ์•ž์˜ ์˜ˆ์ œ์—์„œ๋Š” db.mushrooms ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์—์„œ ๋Œ€๋Ÿ‰ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•œ ๋ฒˆ์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋กœ ์—ฌ๋Ÿฌ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๋Œ€๋Ÿ‰ ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” ingredients.sweet ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์— ๋ฌธ์„œ ํ•˜๋‚˜๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ  meals.dessert ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์— ๋ฌธ์„œ ํ•˜๋‚˜๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

let sweet: Collection<Document> = client
.database("ingredients")
.collection("sweet");
let dessert: Collection<Document> = client
.database("meals")
.collection("dessert");
let models = vec![
InsertOneModel::builder()
.namespace(sweet.namespace())
.document(doc! { "name": "brown sugar", "price": 3.99 })
.build(),
InsertOneModel::builder()
.namespace(dessert.namespace())
.document(doc! { "name": "banana bread", "cook_time": 75 })
.build(),
];
let result = client.bulk_write(models).await?;
println!("Inserted documents: {}", result.inserted_count);
Inserted documents: 2

๋Œ€๋Ÿ‰ ์ž‘์—…์— ํ•™์Šต ๋ณด๋ ค๋ฉด ์„œ๋ฒ„ ๋งค๋‰ด์–ผ์˜ ๋Œ€๋Ÿ‰ ์“ฐ๊ธฐ ์ž‘์—… ์„ ์ฐธ์กฐํ•˜์„ธ์š”.