- MongoDB 教程
- MongoDB - 首页
- MongoDB - 概述
- MongoDB - 优势
- MongoDB - 环境配置
- MongoDB - 数据建模
- MongoDB - 创建数据库
- MongoDB - 删除数据库
- MongoDB - 创建集合
- MongoDB - 删除集合
- MongoDB - 数据类型
- MongoDB - 插入文档
- MongoDB - 查询文档
- MongoDB - 更新文档
- MongoDB - 删除文档
- MongoDB - 投影
- MongoDB - 限制记录
- MongoDB - 排序记录
- MongoDB - 索引
- MongoDB - 聚合
- MongoDB - 复制
- MongoDB - 分片
- MongoDB - 创建备份
- MongoDB - 部署
- MongoDB - Java
- MongoDB - PHP
- 高级 MongoDB
- MongoDB - 关系
- MongoDB - 数据库引用
- MongoDB - 覆盖查询
- MongoDB - 查询分析
- MongoDB - 原子操作
- MongoDB - 高级索引
- MongoDB - 索引限制
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - 文本搜索
- MongoDB - 正则表达式
- 使用 Rockmongo
- MongoDB - GridFS
- MongoDB - 封顶集合
- 自动递增序列
- MongoDB 有用资源
- MongoDB - 问答
- MongoDB - 快速指南
- MongoDB - 有用资源
- 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: "https://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: "https://tutorialspoint.com",
tags: ["mongodb", "database", "NoSQL"],
likes: 100
},
{
title: "NoSQL Database",
description: "NoSQL database doesn't have tables",
by: "tutorials point",
url: "https://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,则它将替换包含在 save() 方法中指定的 _id 的文档的全部数据。
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")
]
}
>
广告