MongoDB学习四--MongoDB插入数据详情


1,插入文档

In MongoDB, the db.collection.insert() method adds new documents into a collection.

插入文档到一个集合中,如果集合不存在,MongoDB将会自动创建集合,不用手动创建。

db.admin.insert(
   {
     item: "ABC1",
     details: {
        model: "14Q3",
        manufacturer: "XYZ Company"
     },
     stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ],
     category: "clothing"
   }
)

插入操作将返回一个带有操作状态的WriteResult对象,成功的插入操作返回的数据如下所示:

WriteResult({ "nInserted" : 1 })
The nInserted field specifies the number of documents inserted. If the operation encounters an error, the WriteResult object will contain the error information.

2,查询插入对象

db.admin.find()
查询结果如下所示:

{ "_id" : ObjectId("53d98f133bb604791249ca99"), "item" : "ABC1", "details" : { "model" : "14Q3", "manufacturer" : "XYZ Company" }, "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ], "category" : "clothing" }
查出来的数据有一列 “_id": 这个字段是数据库默认给我们加的GUID,目的就是保证数据的唯一性。

2,插入文档数组,首先需要组装数据

var mydocuments =
    [
      {
        item: "ABC2",
        details: { model: "14Q3", manufacturer: "M1 Corporation" },
        stock: [ { size: "M", qty: 50 } ],
        category: "clothing"
      },
      {
        item: "MNO2",
        details: { model: "14Q3", manufacturer: "ABC Company" },
        stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 }, { size: "L", qty: 1 } ],
        category: "clothing"
      },
      {
        item: "IJK2",
        details: { model: "14Q2", manufacturer: "M5 Corporation" },
        stock: [ { size: "S", qty: 5 }, { size: "L", qty: 1 } ],
        category: "houseware"
      }
    ];
再执行插入

db.admin.insert( mydocuments );
批量插入将返回一个 带有操作状态的 BulkWriteResult  对象. 结果如下:

BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})
The  nInserted  field specifies the number of documents inserted. If the operation encounters an error, the  BulkWriteResult  object will contain information regarding the error.

3,使用Bulk插入多文档数组

MongoDB provides a Bulk() API that you can use to perform multiple write operations in bulk. The following sequence of operations describes how you would use the Bulk() API to insert a group of documents into a MongoDB collection.

a,Initialize a Bulk operations builder

var bulk = db.admin.initializeUnorderedBulkOp();
The operation returns an unordered operations builder which maintains a list of operations to perform. Unordered operations means that MongoDB can execute in parallel as well as in nondeterministic order. If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.

b,增加和执行插入操作给bulk对象

bulk.insert(
   {
     item: "BE10",
     details: { model: "14Q2", manufacturer: "XYZ Company" },
     stock: [ { size: "L", qty: 5 } ],
     category: "clothing"
   }
);
bulk.insert(
   {
     item: "ZYT1",
     details: { model: "14Q1", manufacturer: "ABC Company"  },
     stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 } ],
     category: "houseware"
   }
);
C,执行bulk操作

bulk.execute();
The method returns a  BulkWriteResult  object with the status of the operation. A successful insert of the documents returns the following object:

BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 2,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})
You can also initialize an ordered operations builder; see this URL for details: http://docs.mongodb.org/master/reference/method/db.collection.initializeOrderedBulkOp/#db.collection.initializeOrderedBulkOp




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值