mongodb3.2系统性学习——1、文档插入insert insertOne insertMany

时间:2016-02-20 15:59:43   收藏:0   阅读:14214

mongodb提供以下操作执行添加文档操作

技术分享

语法规则:

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>   //Optional. A document expressing the write concern. Omit to use the default write concern.
   }
)

关于 writeconcern 后边章节会详细介绍

返回值:

A document containing:

注意:

    集合不存在该操作会创建集合。

 该操作不支持 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"
})

 

 

 

语法规则: 

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:

注意:

  1. 每一组操作最大可以有1000个文档。
  2. 顺序添加的速度要慢于不按顺序添加的。
  3. 不存在的集合会自动创建。
  4. insertMany() 也不支持db.collection.explain() 可以使用insert 代替
  5. 如果添加出错会报出 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" })

 

语法:

db.collection.insert(
   <document or array of documents>,  //数组或者文档都可以添加
   {
     writeConcern: <document>,
     ordered: <boolean> //跟insertMany()操作作用一致
   }
)

 

返回值:

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!