MongoDB 插入文档
本章节中我们将向大家介绍如何将数据插入到 MongoDB 的集合中。
文档的数据结构和 JSON 基本一样。
所有存储在集合中的数据都是 BSON 格式。
BSON 是一种类似 JSON 的二进制形式的存储格式,是 Binary JSON 的简称。
常用的插入文档方法包括:
| 方法 | 用途 | 是否弃用 |
|---|---|---|
insertOne() | 插入单个文档 | 否 |
insertMany() | 插入多个文档 | 否 |
insert() | 插入单个或多个文档 | 是 |
save() | 插入或更新文档 | 是 |
1、insertOne()
insertOne() 方法用于在集合中插入单个文档。
db.collection.insertOne(document, options)
- document:要插入的单个文档。
- options(可选):一个可选参数对象,可以包含 writeConcern 和 bypassDocumentValidation 等。
实例
name: "Alice",
age: 25,
city: "New York"
});
返回结果:
{
"acknowledged": true,
"insertedId": ObjectId("60c72b2f9b1d8b5a5f8e2b2d")
}
2、insertMany()
insertMany() 方法用于在集合中插入多个文档。
db.collection.insertMany(documents, options)
- documents:要插入的文档数组。
- options(可选):一个可选参数对象,可以包含 ordered、writeConcern 和 bypassDocumentValidation 等。
db.myCollection.insertMany([
{ name: "Bob", age: 30, city: "Los Angeles" },
{ name: "Charlie", age: 35, city: "Chicago" }
]);返回结果:
{
"acknowledged": true,
"insertedIds": [
ObjectId("60c72b2f9b1d8b5a5f8e2b2e"),
ObjectId("60c72b2f9b1d8b5a5f8e2b2f")
]
}
3、db.collection.save()
save() 方法在插入文档时表现得类似于 insertOne()。
如果文档包含 _id 字段且已存在,则该文档会被更新;如果文档不包含 _id 字段或 _id 不存在,则会插入一个新文档。
从 MongoDB 4.2 开始,db.collection.save() 已被标记为弃用(deprecated),官方推荐使用 db.collection.insertOne() 或 db.collection.replaceOne() 替代。
db.collection.save(document, options)
- document:要保存的文档。
- options(可选):一个可选参数对象,可以包含 writeConcern 等。
db.myCollection.save({
_id: ObjectId("60c72b2f9b1d8b5a5f8e2b2d"),
name: "David",
age: 40,
city: "San Francisco"
});
插入单个或多个文档(旧方法)
使用 db.collection.insert() 方法插入单个或多个文档。
insert() 方法在 MongoDB 4.2 及更高版本中已被标记为弃用(deprecated),推荐使用 insertOne() 和 insertMany()。
语法:
db.collection.insert(document_or_array)
参数:
可以是一个文档,也可以是一个文档数组。
1、插入单个文档:
db.users.insert({ name: "David", age: 40, email: "david@example.com" });
2、插入多个文档:
db.users.insert([
{ name: "Eve", age: 45, email: "eve@example.com" },
{ name: "Frank", age: 50, email: "frank@example.com" }
]);
批量插入的性能优化
如果需要插入大量文档,可以使用 insertMany() 并启用 ordered: false 选项,以提高插入性能。
语法:
db.collection.insertMany([document1, document2, ...], { ordered: false })
参数:
ordered: false:表示无序插入,即使某个文档插入失败,也不会影响其他文档的插入。
实例
{ name: "Henry", age: 60 },
{ name: "Ivy", age: 65 }
], { ordered: false });
插入时的验证
MongoDB 会根据集合的 schema 验证规则(如果定义了)对插入的文档进行验证。如果文档不符合规则,插入操作会失败。
假设集合 users 有一个验证规则,要求 age 字段必须大于 0:
db.createCollection("users", {
validator: {
age: { $gt: 0 }
}
});如果插入以下文档会失败:
db.users.insertOne({ name: "John", age: -5 }); // 验证失败
二少
272***623@qq.com
参考地址
3.2 版本后还有以下几种语法可用于插入文档:
# 插入单条数据 > var document = db.collection.insertOne({"a": 3}) > document { "acknowledged" : true, "insertedId" : ObjectId("571a218011a82a1d94c02333") } # 插入多条数据 > var res = db.collection.insertMany([{"b": 3}, {'c': 4}]) > res { "acknowledged" : true, "insertedIds" : [ ObjectId("571a22a911a82a1d94c02337"), ObjectId("571a22a911a82a1d94c02338") ] }二少
272***623@qq.com
参考地址
Tffans
897***246@qq.com
一次插入多条数据
1、先创建数组
2、将数据放在数组中
3、一次 insert 到集合中
var arr = []; for(var i=1 ; i<=20000 ; i++){ arr.push({num:i}); } db.numbers.insert(arr);Tffans
897***246@qq.com