Insert Documents
Insert a Single Document
db.collection.insertOne() 은 하나의 document를 collection에 추가합니다.
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
insertOne() 메서드는 새롭게 추가한 document를 새롭게 추가된 _id 필드를 추가해서 리턴합니다.
Insert Multiple Documents
db.collection.insertMany() 를 통해서 다수의 document를 collection에 추가할 수 있습니다. docoment를 array에 추가해서 전달하는 방식입니다.
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
Insert Behavior
Collection Creation
_id Field
몽고디비에서 저장되는 document는 _id필드를 primary key로 사용하시 위해 필요로합니다. 만약에 _id필드를 생략하도 document를 추가한다면 자동으로 몽고디비 드라이버가 _id 필드를 생성하고 값에 ObjectId값을 생성해 추가합니다.
수정작업시에도 update:true 값을 설정하면 새로운 document가 추가 됩니다.
Write Acknowledgement
쓰기작업에 있어서 write concern을 통한다면 쓰기 요청의 레벨(level of acknowledgement) 지정할수가 있습니다.