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("507f191e810c19729de860ea"),
... title: "MongoDB オーバービュー",
... description: "MongoDBはsqlデータベースではありません",
... by: "基本チュートリアル",
... url: "https://ja.oldtoolbag.com",
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted": 1 })
>

これは前章で作成したコレクション名です mycol 。そのコレクションがデータベースに存在しない場合、MongoDBはそのコレクションを作成し、その中にドキュメントを挿入します。

ドキュメントに_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://ja.oldtoolbag.com",
		tags: ["mongodb", "database", "NoSQL"],
		likes: 100
	}
	{
	title: "NoSQL Database"
	description: "NoSQL数据库没有表",
	by: "基础教程",
	url: "http://ja.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")
	]
}
>