MongoDB学习笔记(二)- 文档插入
文章目录
单个文档插入操作
定义
db.collection.insertOne()
语法:
1db.collection.insertOne(
2 <document>,
3 {
4 writeConcern: <document>
5 }
6)
参数:
参数 | 类型 | 说明 |
---|---|---|
document | 文档 | 待插入集合的文档 |
writeConcern | 文档 | 可选。用于指定mongod对写操作的响应 |
返回值:
包含下列元素的文档:
acknowledged
- 如果指定了writeConcern
,值为true
;如果writeConcern
是disable的,值为false
insertedId
- 主键,也就是_id
的值
行为
- 执行
insertOne()
时,如果集合(表)不存在,那么会自动创建这个集合 - 如果没有显式地给定
_id
的值,那么mongod
或者客户端驱动会自动创建一个,并且值具有唯一性。如果插入的文档的_id
重复,则插入失败 - 如果在事务中执行插入,那么不要显式地设置
writeConcern
示例
插入的时候未指定主键
1try {
2 db.products.insertOne( { item: "card", qty: 15 } );
3} catch (e) {
4 print (e);
5};
返回值:
1{
2 "acknowledged" : true,
3 "insertedId" : ObjectId("56fc40f9d735c28df206d078")
4}
插入的时候指定主键
1db.products.insertOne({_id: 10, item: "card", qty: 15})
如果指定的值为10主键_id
已经存在,这时候插入报类似下面的错误:
1{
2 "index" : 0,
3 "code" : 11000,
4 "errmsg" : "E11000 duplicate key error collection: jonlimx_db.products index: _id_ dup key: { _id: 11.0 }",
5 "op" : {
6 "_id" : 11,
7 "item" : "card",
8 "qty" : 15
9 }
10}
多个文档插入
定义
db.collection.insertMany()
语法:
1db.collection.insertMany(
2 [ <document 1> , <document 2>, ... ],
3 {
4 writeConcern: <document>,
5 ordered: <boolean>
6 }
7)
参数:
参数 | 类型 | 说明 |
---|---|---|
[ <document 1> , <document 2>, ... ] | 文档 | 数组,待插入集合的文档列表 |
writeConcern | 文档 | 可选。用于指定mongod对写操作的响应 |
ordered | 布尔 | 可选。指定mongod有序还是无序插入。默认值是true |
返回值:
包含下列元素的文档:
acknowledged
- 如果指定了writeConcern
,值为true
;如果writeConcern
是disable的,值为false
insertedIds
- 主键数组,也就是各个文档的_id
的值集合
行为
- 执行
insertMany()
时,如果集合(表)不存在,那么会自动创建这个集合 - 如果没有显式地给定
_id
的值,那么mongod
或者客户端驱动会自动创建一个,并且值具有唯一性。如果插入的文档的_id
重复,则插入失败 - 默认情况下,
mongod
按顺序插入。如果ordered
设为false,那么mongod
将以无序(或者按照性能优化重新排序)的方式插入 - 如果是有序的操作,后一个操作需要等前一个操作完成了再进行
- 如果有序操作,当前操作发生错误,那么后面的操作则停止;如果是无序操作,当前操作失败不影响后续操作进行
- 数据库通过
maxWriteBatchSize
控制每组最大的写操作次数,如果insertMany
批量插入的时候超过这个值,一般来说客户端驱动会把该组操作分解成多组
示例
插入的时候未指定主键
1try {
2 db.products.insertMany( [
3 { item: "card", qty: 15 },
4 { item: "envelope", qty: 20 },
5 { item: "stamps" , qty: 30 }
6 ] );
7} catch (e) {
8 print (e);
9}
返回值:
1{
2 "acknowledged" : true,
3 "insertedIds" : [
4 ObjectId("61937128eae4d2a457051eea"),
5 ObjectId("61937128eae4d2a457051eeb"),
6 ObjectId("61937128eae4d2a457051eec")
7 ]
8}
插入的时候指定重复主键
1db.products.insertMany( [
2 {_id:ObjectId("61937128eae4d2a457051eea"),item: "card", qty: 15 },
3 { item: "envelope", qty: 20 },
4 { item: "stamps" , qty: 30 }
5 ] );
由于键值为61937128eae4d2a457051eea
的文档已经存在,导致该文档插入失败;又因为ordered
默认值为true
,后续的插入操作停止
返回值:
1{
2 "writeErrors" : [
3 {
4 "index" : 0,
5 "code" : 11000,
6 "errmsg" : "E11000 duplicate key error collection: jonlimx_db.products index: _id_ dup key: { _id: ObjectId('61937128eae4d2a457051eea') }",
7 "op" : {
8 "_id" : ObjectId("61937128eae4d2a457051eea"),
9 "item" : "card",
10 "qty" : 15
11 }
12 }
13 ],
14 "writeConcernErrors" : [ ],
15 "nInserted" : 0,
16 "nUpserted" : 0,
17 "nMatched" : 0,
18 "nModified" : 0,
19 "nRemoved" : 0,
20 "upserted" : [ ]
21}
插入的时候指定重复主键且ordered=false
1db.products.insertMany( [
2 {_id:ObjectId("61937128eae4d2a457051eea"),item: "card", qty: 15 },
3 { item: "envelope", qty: 20 },
4 { item: "stamps" , qty: 30 }
5 ] ,
6 {ordered: false});
返回值 - 第一条插入失败,后面两条插入成功
1{
2 "writeErrors" : [
3 {
4 "index" : 0,
5 "code" : 11000,
6 "errmsg" : "E11000 duplicate key error collection: jonlimx_db.products index: _id_ dup key: { _id: ObjectId('61937128eae4d2a457051eea') }",
7 "op" : {
8 "_id" : ObjectId("61937128eae4d2a457051eea"),
9 "item" : "card",
10 "qty" : 15
11 }
12 }
13 ],
14 "writeConcernErrors" : [ ],
15 "nInserted" : 2,
16 "nUpserted" : 0,
17 "nMatched" : 0,
18 "nModified" : 0,
19 "nRemoved" : 0,
20 "upserted" : [ ]
21}