db.collection.insertMany()
คำสั่ง db.collection.insertMany()
เป็นคำสั่งสำหรับ เพิ่มข้อมูลหลาย ๆ ข้อมูล เข้าไปใน collection
อ้างอิงจาก ลิงค์นี้
ข้อมูลที่ Return
Section titled “ข้อมูลที่ Return”เมื่อใช้คำสั่งนี้จะข้อมูลที่ Return มาจะมี
acknowledged
มีค่าเป็น BooleaninsertedIds
ซึ่งจะมีค่าเป็น array ที่มีค่าเป็นรายการ_id
ของข้อมูลที่เราเพิ่มเข้าไป
Syntax
Section titled “Syntax”db.collection.insertMany( [ <document 1> , <document 2>, ... ], { writeConcern: <document>, ordered: <boolean> })
Parameter | ชนิดข้อมูล | คำอธิบาย |
---|---|---|
document | document | เป็น array ของข้อมูลที่ต้องการเพิ่มเข้าไป |
writeConcern | document | ไม่จำเป็นต้องระบุ (Optional) - สามารถดูการตั้งค่าเพิ่มเติมในนี้ได้ครับ |
ordered | boolean | ไม่จำเป็นต้องระบุ (Optional) - มีไว้เพื่อระบุว่าการแทรกข้อมูลจะถูกดำเนินการตามลำดับหรือไม่ (ค่าเริ่มต้นเป็น true) |
collection จะถูกสร้างมาให้โดยอัตโนมัติ ถ้าหากเราใช้คำสั่งนี้โดยที่ยังไม่ได้สร้าง collection
โดยทั่วไปการเพิ่มข้อมูลจะถูกแทรกตามลำดับที่กำหนดไว้ สิ่งที่ควรรู้เพิ่มเติมคือ
ถ้าหากเรากำหนด ordered เป็น true
แล้วเกิดข้อผิดพลาดขณะที่เพิ่มข้อมูล ข้อมูลที่อยู่หลังจากข้อมูลที่ผิดพลาดจะไม่ถูกเพิ่มเข้าไป
แต่ถ้าหากเรากำหนด ordered เป็น false
แล้วเกิดข้อผิดพลาดขณะที่เพิ่มข้อมูล ข้อมูลที่ไม่ได้เกิดปัญหาจะถูกเพิ่มเข้าไปปกติ แต่ข้อมูลที่เกิดปัญหาจะไม่ถูกเพิ่มเข้าไป
Note เพิ่มเติม
จำนวนของข้อมูลจะต้องไม่เกินค่า
maxWriteBatchSize
ซึ่งเราสามารถดูค่านี้โดยการใช้คำสั่งdb.hello()
ซึ่งจะมีคีย์maxWriteBatchSize
แสดงอยู่ โดยปกติแล้วค่าจะอยู่ที่ 100,000 (จำนวน array ของข้อมูลต่อการเขียน 1 ครั้ง)
เพื่อความเข้าใจมากยิ่งขึ้น เรามาดูตัวอย่างกันครับ
Example
Section titled “Example”เพิ่มข้อมูลโดยที่ไม่ได้ระบุ _id
Section titled “เพิ่มข้อมูลโดยที่ไม่ได้ระบุ _id”db.users.insertMany([ { "name": "Person name 1", "position": "Developer", "age": 25, "education": [ { 2021: "Diploma" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-01-01") }, { "name": "Person name 2", "position": "Developer", "age": 26, "education": [ { 2021: "Vocational school" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-02-01") }, { "name": "Person name 3", "position": "Senior Developer", "age": 30, "education": [ { 2021: "Bachelor" }, { 2022: "Master" } ], "createdby": ISODate("2023-02-03") }]);
จะได้ response กลับมา
{ acknowledged: true, insertedIds: { '0': ObjectId("653390650e1875da811cc783"), '1': ObjectId("653390650e1875da811cc784"), '2': ObjectId("653390650e1875da811cc785") }}
เนื่องจากเราไม่ได้ระบุ
_id
ทำให้ mongod สร้าง_id
ให้อัตโนมัติซึ่งจะเป็นค่าObjectId()
ที่มีค่าไม่ซ้ำกับตัวอื่นขึ้นมาครับ ถ้าหากคุณผู้อ่านได้_id
ไม่ตรงกับตัวอย่างในนี้ก็ไม่ต้องตกใจนะครับ เพราะระบบจะสร้างให้มาแบบไม่ซ้ำ
เพิ่มข้อมูลโดยระบุ _id
Section titled “เพิ่มข้อมูลโดยระบุ _id”db.users.insertMany([ { "_id": 1, "name": "Person name 1", "position": "Developer", "age": 25, "education": [ { 2021: "Diploma" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-01-01") }, { "_id": 2, "name": "Person name 2", "position": "Developer", "age": 26, "education": [ { 2021: "Vocational school" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-02-01") }, { "_id": 3, "name": "Person name 3", "position": "Senior Developer", "age": 30, "education": [ { 2021: "Bachelor" }, { 2022: "Master" } ], "createdby": ISODate("2023-02-03") }]);
จะได้ response กลับมา
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 }}
เพิ่มข้อมูลโดยระบุ _id ซ้ำ โดยกำหนดค่า ordered เป็น true
Section titled “เพิ่มข้อมูลโดยระบุ _id ซ้ำ โดยกำหนดค่า ordered เป็น true”ในที่นี้จะกำหนดค่า _id
เป็น 11 ซ้ำกัน 2 อันเน้อครับ และเราจะไม่ตั้งค่า ordered เพราะ ค่าเริ่มต้นของ ordered
มีค่าเป็น true อยู่แล้ว
db.users.insertMany([ { "_id": 11, "name": "Person name 1", "position": "Developer", "age": 25, "education": [ { 2021: "Diploma" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-01-01") }, { "_id": 11, "name": "Person name 2", "position": "Developer", "age": 26, "education": [ { 2021: "Vocational school" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-02-01") }, { "_id": 12, "name": "Person name 3", "position": "Senior Developer", "age": 30, "education": [ { 2021: "Bachelor" }, { 2022: "Master" } ], "createdby": ISODate("2023-02-03") }]);
เมื่อลองใช้คำสั่งข้างต้น จะพบว่าเกิด Error แบบนี้ครับ
MongoBulkWriteError: E11000 duplicate key error collection: example.users index: _id_ dup key: { _id: 11 }Result: BulkWriteResult { insertedCount: 1, matchedCount: 0, modifiedCount: 0, deletedCount: 0, upsertedCount: 0, upsertedIds: {}, insertedIds: { '0': 11, '1': 11, '2': 12 }}Write Errors: [ WriteError { err: { index: 1, code: 11000, errmsg: 'E11000 duplicate key error collection: example.users index: _id_ dup key: { _id: 11 }', errInfo: undefined, op: { _id: 11, name: 'Person name 2', position: 'Developer', age: 26, education: [ { '2021': 'Vocational school' }, { '2022': 'Bachelor' } ], createdby: ISODate("2023-02-01T00:00:00.000Z") } } }]
และจะพบว่าข้อมูลที่ถูกเพิ่มเข้าไปจะมีแค่ _id: 11
อันเดียว ซึ่งคือข้อมูลแรกที่เพิ่มเข้าไป และยังไม่ได้เกิดปัญหาอะไร แต่เนื่องจากข้อมูลถัดมาคือเกิดข้อผิดพลาด คือ _id: 11
ตัวถัดมาไม่สามารถเพิ่มได้ ทำให้ _id: 12
จะไม่ถูกเพิ่มต่อแล้ว
เพิ่มข้อมูลโดยระบุ _id
ซ้ำ โดยกำหนดค่า ordered เป็น false
Section titled “เพิ่มข้อมูลโดยระบุ _id ซ้ำ โดยกำหนดค่า ordered เป็น false”ในที่นี้เราจะลองกำหนดค่าให้คล่้าย ๆ กับก่อนหน้านี้ โดยจะมี _id: 21
ซ้ำกัน โดยกำหนดค่า ordered
เป็น false
db.users.insertMany([ { "_id": 21, "name": "Person name 1", "position": "Developer", "age": 25, "education": [ { 2021: "Diploma" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-01-01") }, { "_id": 21, "name": "Person name 2", "position": "Developer", "age": 26, "education": [ { 2021: "Vocational school" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-02-01") }, { "_id": 22, "name": "Person name 3", "position": "Senior Developer", "age": 30, "education": [ { 2021: "Bachelor" }, { 2022: "Master" } ], "createdby": ISODate("2023-02-03") }], { ordered: false });
เมื่อลองใช้คำสั่งข้างต้นจะพบว่าแสดง error กลับมาดังนี้ครับ
MongoBulkWriteError: E11000 duplicate key error collection: example.users index: _id_ dup key: { _id: 21 }Result: BulkWriteResult { insertedCount: 2, matchedCount: 0, modifiedCount: 0, deletedCount: 0, upsertedCount: 0, upsertedIds: {}, insertedIds: { '0': 21, '1': 21, '2': 22 }}Write Errors: [ WriteError { err: { index: 1, code: 11000, errmsg: 'E11000 duplicate key error collection: example.users index: _id_ dup key: { _id: 21 }', errInfo: undefined, op: { _id: 21, name: 'Person name 2', position: 'Developer', age: 26, education: [ { '2021': 'Vocational school' }, { '2022': 'Bachelor' } ], createdby: ISODate("2023-02-01T00:00:00.000Z") } } }]
หากเราดูข้อมูลที่ถูกเพิ่มเข้าไปแล้ว จะพบว่า _id: 21
(Person name 1) และ _id: 22
(Person name 3) จะถูกเพิ่มเข้าไป