mongodb3.2系统性学习——1文档插入insert insertOne insertMany
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb3.2系统性学习——1文档插入insert insertOne insertMany相关的知识,希望对你有一定的参考价值。
- 写操作——添加操作
mongodb提供以下操作执行添加文档操作
- db.collection.insertOne() 3.2新添加
- db.collection.insertMany() 3.2 新添加
- db.collection.insert()
- 首先介绍下 insertone() 操作
语法规则:
db.collection.insertOne( <document>, { writeConcern: <document> //Optional. A document expressing the write concern. Omit to use the default write concern. } )
关于 writeconcern 后边章节会详细介绍
返回值:
A document containing:
- A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled.
- A single element insertedId with the _id of the inserted document.
注意:
集合不存在该操作会创建集合。
该操作不支持 db.collection.explain() 查询计划 可以使用insert 代替。
实例:
try { db.products.insertOne( { item: "card", qty: 15 } ); }; catch (e) { print (e); };
返回值:
{ "acknowledged" : true, "insertedIds" : [ ObjectId("562a94d381cb9f1cd6eb0e1a"), ] }
添加write concern选项之后 复制集
try { db.products.insertOne( { "item": "envelopes", "qty": 100, type: "Self-Sealing" }, { writeConcern: { w : "majority", wtimeout : 100 } }; //w:majority,表示>1/2的节点有数据 ) } catch (e) { print (e); }
如果超时返回结果实例
WriteConcernError({ "code" : 64, "errInfo" : { "wtimeout" : true }, "errmsg" : "waiting for replication timed out" })
- 介绍下 inserMany() 操作 该操作是 3.2版本新添加的功能
语法规则:
db.collection.insertMany( { [ <document 1> , <document 2>, ... ] },//An array of documents to insert into the collection. 注意是数组 { writeConcern: <document>, ordered: <boolean> //Optional. A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults to true. 默认式按照顺序添加的 顺序添加的速度要慢于不按顺序添加的 } )
返回值:
A document containing:
- A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled
- An array of _id for each successfully inserted documents
注意:
- 每一组操作最大可以有1000个文档。
- 顺序添加的速度要慢于不按顺序添加的。
- 不存在的集合会自动创建。
- insertMany() 也不支持db.collection.explain() 可以使用insert 代替。
- 如果添加出错会报出 BulkWriteError exception 异常,按照顺序添加的 操作遇到错误会直接停止,而不按照顺序的会继续执行在队列中的写操作。
实例:不指定 _id
try { db.products.insertMany( [ { item: "card", qty: 15 }, { item: "envelope", qty: 20 }, { item: "stamps" , qty: 30 } ] ); } catch (e) { print (e); }
返回值:
{ "acknowledged" : true, "insertedIds" : [ ObjectId("562a94d381cb9f1cd6eb0e1a"), ObjectId("562a94d381cb9f1cd6eb0e1b"), ObjectId("562a94d381cb9f1cd6eb0e1c") ] }
指定_id字段:
try { db.products.insertMany( [ { _id: 10, item: "large box", qty: 20 }, { _id: 11, item: "small box", qty: 55 }, { _id: 12, item: "medium box", qty: 30 } ] ); } catch (e) { print (e); }
返回值:
{ "acknowledged" : true, "insertedIds" : [ 10, 11, 12 ] }
插入一个_id字段重复的字段:
try { db.products.insertMany( [ { _id: 13, item: "envelopes", qty: 60 }, { _id: 13, item: "stamps", qty: 110 }, { _id: 14, item: "packing tape", qty: 38 } ] ); } catch (e) { print (e); }
返回值: 返回 BulkWriteError 异常 只会添加第一条数据
BulkWriteError({ "writeErrors" : [ { "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: restaurant.test index: _id_ dup key: { : 13.0 }", "op" : { "_id" : 13, "item" : "envelopes", "qty" : 60 } } ], "writeConcernErrors" : [ ], "nInserted" : 0, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
没有顺序添加操作:
try { db.products.insertMany( [ { _id: 10, item: "large box", qty: 20 }, { _id: 11, item: "small box", qty: 55 }, { _id: 11, item: "medium box", qty: 30 }, { _id: 12, item: "envelope", qty: 100}, { _id: 13, item: "stamps", qty: 125 }, { _id: 13, item: "tape", qty: 20}, { _id: 14, item: "bubble wrap", qty: 30} ], { ordered: false } ); } catch (e) { print (e); }
返回值: 即使出现错误后续的字段也能正常添加进
BulkWriteError({ "writeErrors" : [ { "index" : 2, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 11.0 }", "op" : { "_id" : 11, "item" : "medium box", "qty" : 30 } }, { "index" : 5, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", "op" : { "_id" : 13, "item" : "tape", "qty" : 20 } } ], "writeConcernErrors" : [ ], "nInserted" : 5, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
带着 写入安全级别writeconcern 跟 wtimeout
try { db.products.insertMany( [ { _id: 10, item: "large box", qty: 20 }, { _id: 11, item: "small box", qty: 55 }, { _id: 12, item: "medium box", qty: 30 } ], { w: "majority", wtimeout: 100 } ); } catch (e) { print (e); }
返回值: If the primary and at least one secondary acknowledge each write operation within 100 milliseconds
{ "acknowledged" : true, "insertedIds" : [ ObjectId("562a94d381cb9f1cd6eb0e1a"), ObjectId("562a94d381cb9f1cd6eb0e1b"), ObjectId("562a94d381cb9f1cd6eb0e1c") ] }
超时返回:
- WriteConcernError({ "code" : 64, "errInfo" : { "wtimeout" : true }, "errmsg" : "waiting for replication timed out" })
- 介绍下 inser() 操作
语法:
db.collection.insert( <document or array of documents>, //数组或者文档都可以添加 { writeConcern: <document>, ordered: <boolean> //跟insertMany()操作作用一致 } )
返回值:
- A WriteResult object for single inserts.
- A BulkWriteResult object for bulk inserts.
以上是关于mongodb3.2系统性学习——1文档插入insert insertOne insertMany的主要内容,如果未能解决你的问题,请参考以下文章