๋๋ ์์
์ด ํ์ด์ง์ ๋ด์ฉ
๊ฐ์
์ด ๊ฐ์ด๋ ์์๋ 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()
ํธ์ถ์ ์ ๋ฌํ๋ ์์ ๋ฅผ ๋ณด๋ ค๋ฉด ์ด ๊ฐ์ด๋ ์ ๋์ ์์ ์์ ๋ฅผ ์ฐธ์กฐํ์ธ์.
Insert
๋๋ ์ฝ์
์์
์ ์ํํ๋ ค๋ฉด ์ฝ์
ํ๋ ค๋ ๊ฐ ๋ฌธ์ ์ ๋ํด InsertOneModel
์ธ์คํด์ค ๋ฅผ ๋ง๋ญ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๋ชจ๋ธ ๋ชฉ๋ก์ bulk_write()
๋ฉ์๋์ ์ ๋ฌํฉ๋๋ค.
๋ค์ ํ์์๋ ํด๋น ๋น๋ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ค์ ํ๋ค ์ ์๋ InsertOneModel
ํ๋์ ๋ํด ์ค๋ช
ํฉ๋๋ค.
ํ๋ | ์ค๋ช
|
---|---|
| The namespace on which the insert is performed. Type: Namespace |
| 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
๊ตฌ์กฐ์ฒด ์ ํ์ ์ธ์คํด์ค๋ฅผ ์ฝ์
ํฉ๋๋ค.
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
ํ๋์ ๋ํด ์ค๋ช
ํฉ๋๋ค.
ํ๋ | ์ค๋ช
|
---|---|
| The namespace on which the operation is performed. Type: Namespace |
| The filter that matches the document you want to replace. Type: Document |
| The replacement document. Type: Document |
| (Optional) The collation to use when sorting results. To learn more about collations,
see the Collations guide. Type: Document |
| (Optional) The index to use for the operation. To learn more about
indexes, see the Indexes guide. Type: Bson |
| (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
Update
์ผ๊ด ์
๋ฐ์ดํธ ์์
์ ์ํํ๋ ค๋ฉด ์
๋ฐ์ดํธํ๋ ค๋ ๊ฐ ์
๋ฐ์ดํธ ์ ๋ํด UpdateOneModel
๋๋ UpdateManyModel
์ธ์คํด์ค ๋ฅผ ๋ง๋ญ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๋ชจ๋ธ ๋ชฉ๋ก์ bulk_write()
๋ฉ์๋์ ์ ๋ฌํฉ๋๋ค. UpdateOneModel
์ ํํฐํ๋ค ์ ์ผ์นํ๋ ํ๋์ ๋ฌธ์ ๋ง ์
๋ฐ์ดํธํ๊ณ UpdateManyModel
๋ ํํฐํ๋ค ์ ์ผ์นํ๋ ๋ชจ๋ ๋ฌธ์๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
๋ค์ ํ์์๋ ํด๋น ๋น๋ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ค์ ํ๋ค ์ ์๋ UpdateOneModel
๋ฐ UpdateManyModel
ํ๋์ ๋ํด ์ค๋ช
ํฉ๋๋ค.
ํ๋ | ์ค๋ช
|
---|---|
| The namespace on which the operation is performed. Type: Namespace |
| 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 |
| The update to perform. Type: UpdateModifications |
| (Optional) A set of filters specifying which array elements an update applies to if you are updating an
array-valued field. Type: Array |
| (Optional) The collation to use when sorting results. To learn more about collations,
see the Collations guide. Type: Document |
| (Optional) The index to use for the operation. To learn more about
indexes, see the Indexes guide. Type: Bson |
| (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
ํ๋์ ๋ํด ์ค๋ช
ํฉ๋๋ค.
ํ๋ | ์ค๋ช
|
---|---|
| The namespace on which the operation is performed. Type: Namespace |
| 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 |
| (Optional) The collation to use when sorting results. To learn more about collations,
see the Collations guide. Type: Document |
| (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
๊ตฌ์กฐ์ฒด์๋ ๋ค์ ํ๋๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
ํ๋ | ์ค๋ช
| ๊ธฐ๋ณธ๊ฐ |
---|---|---|
| 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 |
|
| Whether document-level validation is bypassed. Type: bool |
|
| An arbitrary comment to help trace the operation through the database profiler, currentOp,
and logs. Type: Bson |
|
| 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 |
|
| 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
์ถ๊ฐ ์ ๋ณด
๋๋ ์์ ์ ํ์ต ๋ณด๋ ค๋ฉด ์๋ฒ ๋งค๋ด์ผ์ ๋๋ ์ฐ๊ธฐ ์์ ์ ์ฐธ์กฐํ์ธ์.