English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MongoDB 문서 삽입

이 장에서는 MongoDB 콜렉션에 문서를 삽입하는 방법을 배웁니다.

insert() 메서드

MongoDB 콜렉션에 데이터를 삽입하려면 MongoDB의insert() 또는 save()메서드.

문법

insert()명령어의 기본 문법은 다음과 같습니다-

> db.COLLECTION_NAME.insert(document)

온라인 예제

> db.users.insert({
... _id : ObjectId("507f191e810c19729860ea
... title: "MongoDB 개요",
... description: "MongoDB는 sql 데이터베이스가 아닙니다",
... by: "기본 가이드",
... url: "https://ko.oldtoolbag.com",
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted": 1 })
>

이는 이전 장에서 생성한 콜렉션 이름입니다 mycol 데이터베이스에 해당 콜렉션이 없으면 MongoDB가 해당 콜렉션을 생성한 후 문서를 그 안에 삽입합니다.

insert된 문서에 _id 파라미터를 지정하지 않으면 MongoDB가 해당 문서에 유일한 ObjectId를 할당합니다.

_id는12바이트의 16진수 숫자는 콜렉션 내의 각 문서에 대해 유일합니다.12바이트는 다음과 같이 구분됩니다:

_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)

문서 배열을 insert() 메서드에 전달할 수도 있습니다. 예를 들어:

> db.createCollection("post")
> db.post.insert([
	{
		title: "MongoDB 개요",
		description: "MongoDB는 SQL 데이터베이스가 아닙니다",
		by: "基础教程",
		url: "http://ko.oldtoolbag.com",
		tags: ["mongodb", "database", "NoSQL"],
		likes: 100
	},
	{
	title: "NoSQL Database"
	description: "NoSQL数据库没有表",
	by: "基础教程",
	url: "http://ko.oldtoolbag.com",
	tags: ["mongodb", "database", "NoSQL"],
	likes: 20,
	comments: [
		{
			user:"user1",
			message: "My first comment",
			dateCreated: new Date(2013,11,10,2,35),
			like: 0
		}
	]
}
})
BulkWriteResult({
	"writeErrors" : [],
	"writeConcernErrors" : [],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [],
})
>

문서를 삽입하려면 다음과 같이 사용할 수도 있습니다.db.post.save(document)를 지정하지 않으면 _id하면 save() 메서드과 함께insert() 메서드동일합니다. _id를 지정하면 save() 메서드에 지정된 _id를 포함한 문서 전체를 대체합니다.

insertOne() 메서드

한 개의 문서만 삽입하려면 이 메서드를 사용할 수 있습니다.

문법

insertOne() 명령어의 기본 문법은 다음과 같습니다:

>db.COLLECTION_NAME.insertOne(document)

예제

다음 예제에서 새로운 콜렉션 empDetails를 생성하고 insertOne() 메서드를 사용하여 문서를 삽입합니다.

> db.createCollection("empDetails")
{ "ok" : 1 }
> db.empDetails.insertOne(
	{
		First_Name: "Radhika",
		Last_Name: "Sharma",
		Date_Of_Birth: "1995-09-26",
		e_mail: "[email protected]",
		phone: "9848022338"
	})
{
	"acknowledged": true,
	"insertedId" : ObjectId("5dd62b4070fb13eec3963bea")
}
>

insertMany() 메서드

여러 문서를 삽입하려면 insertMany() 메서드를 사용할 수 있습니다. 이 메서드는 문서 배열을 전달해야 합니다.

예제

다음 예제에서 insertMany() 메서드를 사용하여 세 가지 다른 문서를 empDetails 콜렉션에 삽입합니다.

> db.empDetails.insertMany(
	[
		{
			First_Name: "Radhika",
			Last_Name: "Sharma",
			Date_Of_Birth: "1995-09-26",
			e_mail: "[email protected]",
			phone: "9000012345"
		},
		{
			First_Name: "Rachel",
			Last_Name: "Christopher",
			Date_Of_Birth: "1990-02-16",
			e_mail: "[email protected]",
			phone: "9000054321"
		},
		{
			First_Name: "Fathima",
			Last_Name: "Sheik",
			Date_Of_Birth: "1990-02-16",
			e_mail: "[email protected]",
			phone: "9000054321"
		}
	]
)
{
	"acknowledged": true,
	"insertedIds": [
		ObjectId("5dd631f270fb13eec3963bed"),
		ObjectId("5dd631f270fb13eec3963bee"),
		ObjectId("5dd631f270fb13eec3963bef")
	]
}
>