Mongodb 简明教程

MongoDB - Insert Document

在本教程中,我们将学习如何将文档插入到 MongoDB 集合中。

In this chapter, we will learn how to insert document in MongoDB collection.

The insert() Method

要将数据插入到 MongoDB 集合中,需要使用 MongoDB 的 insert()save() 方法。

To insert data into MongoDB collection, you need to use MongoDB’s insert() or save() method.

Syntax

insert() 命令的基本语法如下:

The basic syntax of insert() command is as follows −

>db.COLLECTION_NAME.insert(document)

Example

> db.users.insert({
... _id : ObjectId("507f191e810c19729de860ea"),
... title: "MongoDB Overview",
... description: "MongoDB is no sql database",
... by: "tutorials point",
... url: "http://www.tutorialspoint.com",
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })
>

此处的 mycol 名称是我们在前一章中创建的集合名称。如果集合在数据库中不存在,则 MongoDB 将创建该集合,然后向其中插入文档。

Here mycol is our collection name, as created in the previous chapter. If the collection doesn’t exist in the database, then MongoDB will create this collection and then insert a document into it.

在已插入的文档中,如果我们不指定 _id 参数,则 MongoDB 将为该文档分配一个唯一的 ObjectId。

In the inserted document, if we don’t specify the _id parameter, then MongoDB assigns a unique ObjectId for this document.

_id 是 12 字节十六进制数字,对集合中的每个文档都是唯一的。12 字节的划分如下:

_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows −

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

您还可以将文档数组传递到 insert() 方法中,如下所示:

You can also pass an array of documents into the insert() method as shown below:.

> db.createCollection("post")
> db.post.insert([
	{
		title: "MongoDB Overview",
		description: "MongoDB is no SQL database",
		by: "tutorials point",
		url: "http://www.tutorialspoint.com",
		tags: ["mongodb", "database", "NoSQL"],
		likes: 100
	},
	{
	title: "NoSQL Database",
	description: "NoSQL database doesn't have tables",
	by: "tutorials point",
	url: "http://www.tutorialspoint.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,它将替换包含 _id 的文档的所有数据,如 save() 方法中所指定的那样。

To insert the document you can use db.post.save(document) also. If you don’t specify _id in the document then save() method will work same as insert() method. If you specify _id then it will replace whole data of document containing _id as specified in save() method.

The insertOne() method

如果您只需要向集合中插入一个文档,则可以使用此方法。

If you need to insert only one document into a collection you can use this method.

Syntax

insert() 命令的基本语法如下:

The basic syntax of insert() command is as follows −

>db.COLLECTION_NAME.insertOne(document)

Example

以下示例创建一个名为 empDetails 的新集合,并使用 insertOne() 方法插入一个文档。

Following example creates a new collection named empDetails and inserts a document using the insertOne() method.

> db.createCollection("empDetails")
{ "ok" : 1 }
> db.empDetails.insertOne(
	{
		First_Name: "Radhika",
		Last_Name: "Sharma",
		Date_Of_Birth: "1995-09-26",
		e_mail: "radhika_sharma.123@gmail.com",
		phone: "9848022338"
	})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5dd62b4070fb13eec3963bea")
}
>

The insertMany() method

可以使用 insertMany() 方法插入多个文档。对于此方法,你需要传递一个文档数组。

You can insert multiple documents using the insertMany() method. To this method you need to pass an array of documents.

Example

以下示例使用 insertMany() 方法向 empDetails 集合中插入三个不同的文档。

Following example inserts three different documents into the empDetails collection using the insertMany() method.

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