MongoDB – 插入文档

MongoDB – 插入文档


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

insert() 方法

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

句法

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

>db.COLLECTION_NAME.insert(document)

例子

> 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 将创建该集合,然后将文档插入其中。

在插入的文档中,如果我们不指定_id参数,那么MongoDB会为这个文档分配一个唯一的ObjectId。

_id 是 12 字节的十六进制数,对于集合中的每个文档都是唯一的。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 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() 方法中指定的那样。

insertOne() 方法

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

句法

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

>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: "radhika_sharma.123@gmail.com",
		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: "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")
	]
}
>

觉得文章有用?

点个广告表达一下你的爱意吧 !😁