🌿

4. CRUD-생성(Create)

 
CRUD는 Create, Read, Update, Delete의 약자로 문서를 생성하고 읽고 수정하고 삭제하는 것을 말합니다. 이번에는 컬렉션에 문서를 생성 또는 삽입하는 방법에 대해 알아보겠습니다.
 
데이터를 추가하기 전에 market이라는 데이터베이스를 만들도록 하겠습니다.
use market
 
notion imagenotion image
컬렉션은 user_info 를 만들도록 하겠습니다.
db.createCollection("user_info")
 
컬렉션에 문서를 삽입하는 방법은 3가지가 있습니다.
  • insertOne : 하나의 문서만 삽입할 수 있습니다.
  • insertMany : 여러 가지의 문서를 삽입할 수 있습니다.
  • insert : 하나 또는 여러가지의 문서를 삽입할 수 있습니다.
 
컬렉션에 id가 없는 문서를 삽입해보겠습니다.
db.user_info.insertOne({ "name" : {"first_name" : "kang", "last_name" : "seokho"}, "age" : 23, "gender" : "M", "area" : "jeju", "size" : {"h" : 182, "w" : 76}, "ABO" : "O", "point" : 1000 })
 
아래와 같이 자동으로 아이디를 만들어 주는 것을 볼 수 있습니다.
notion imagenotion image
 
컬렉션에 id가 있는 하나의 문서를 삽입해 보도록 하겠습니다.
db.user_info.insertOne({ _id : 101, "name" : {"first_name" : "kim", "last_name" : "mina"}, "age" : 28, "gender" : "F", "area" : "jeju", "size" : {"h" : 163, "w" : 56}, "ABO" : "A", "point" : 2000, "coupon" : ["50% sale","3000won discount"] })
 
출력을 살펴보도록 하겠습니다.
notion imagenotion image
  • acknowledged : boolean값으로 문서가 삽입이 실행된 경우 true로 생성되고 삽입이 비활성화 된 경우에는 false로 설정됩니다.
  • insertedId : _id 값을 지정하면 지정한 id값이 출력되며, 지정하지 않을 경우 ObjectId 값이 필드를 문서에 추가합니다.
 
💡
_id 필드에 값을 지정해 줄 때에는 값이 컬렉션 내에서 고유한 값인지 확인해야 합니다. 그렇지 않은 경우 중복키 오류가 발생할 수 있습니다.
notion imagenotion image
 
객체와 배열 삽입 방법은 다음과 같습니다.
  • {} - 객체를 표현할 때 사용하는 표기법
  • [] - 배열, 리스트, 문서를 표현하는 표기법
notion imagenotion image
 
데이터베이스에 더 많은 데이터를 추가하기 위해 insertMany() 를 실행해 보도록 하겠습니다 .
db.user_info.insertMany([ {_id : 102, "name" : {"first_name" : "kang", "last_name" : "chulsoo"}, "age" : 25, "gender" : "M", "area" : "seoul", "size" : {"h" : 173, "w" : 67}, "ABO" : "A", "coupon" : ["20%_sale"]}, {_id : 103, "name" : {"first_name" : "kim", "last_name" : "younghee"}, "age" : 18, "gender" : "F", "area" : "jeju", "size" : {"h" : 158, "w" : 50}, "ABO" : "B", "point" : 500}, {_id : 104, "name" : {"first_name" : "oh", "last_name" : "minsoo"}, "age" : 40, "gender" : "M", "area" : "busan", "size" : {"h" : 190, "w" : 71}, "ABO" : "B", "point" : 1500, "coupon" : ["10%_sale", "free_delivery"]}, {_id : 105, "name" : {"first_name" : "lee", "last_name" : "sojin"}, "age" : 35, "gender" : "F", "area" : "jeju", "size" : {"h" : 161, "w" : 55}, "ABO" : "O", "point" : 500}, {_id : 106, "name" : {"first_name" : "hong", "last_name" : "gildong"}, "age" : 45, "gender" : "F", "area" : "seoul", "size" : {"h" : 168, "w" : 65}, "ABO" : "AB", "point" : 500} ]);
notion imagenotion image
 
insertMany로 문서를 삽입할 경우에 order를 false로 설정하여 이용하여_id 값이 중복될 경우, 중복된 데이터만 에러를 출력하고 나머지 데이터는 삽입할 수 있습니다.
nInserted는 값이 들어갔다는 것입니다. 데이터는 2개가 들어갔는데 삽입된 값은 1개입니다.
db.user_info.insert([{_id : 106, "name" : {"first_name" : "hong", "last_name" : "gildong"}, "age" : 45, "gender" : "F", "area" : "seoul", "size" : {"h" : 168, "w" : 65}, "ABO" : "AB", "point" : 500}, {_id : 107, "name" : {"first_name" : "hong", "last_name" : "gildong"}, "age" : 45, "gender" : "F", "area" : "seoul", "size" : {"h" : 168, "w" : 65}, "ABO" : "AB", "point" : 500} ], {ordered : false});
notion imagenotion image
 
또 다른 방법으로 문서를 정의하고 문서를 삽입할 수 있습니다.
var employee=[ { _id : 1001, "name" : {"first_name" : "pack", "last_name" : "sooah"}, "rank" : "manager", "age" : 40}, {_id : 1002, "name" : {"first_name" : "yoon", "last_name" : "sechan"}, "rank" : "staff", "age" : 25} ] db.employee.insert(employee)
 
notion imagenotion image