MongoDB学习四--MongoDB插入数据详情
2015-08-20 23:55
591 查看
1,插入文档
In MongoDB, the
db.collection.insert()
method adds new documents into a collection.
插入文档到一个集合中,如果集合不存在,MongoDB将会自动创建集合,不用手动创建。
插入操作将返回一个带有操作状态的WriteResult对象,成功的插入操作返回的数据如下所示:
The nInserted field
specifies the number of documents inserted. If the operation encounters an error, the WriteResult object
will contain the error information.
2,查询插入对象
查询结果如下所示:
查出来的数据有一列 “_id": 这个字段是数据库默认给我们加的GUID,目的就是保证数据的唯一性。
2,插入文档数组,首先需要组装数据
再执行插入
批量插入将返回一个 带有操作状态的BulkWriteResult 对象.
结果如下:
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
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对象
C,执行bulk操作
The method returns a BulkWriteResult object
with the status of the operation. A successful insert of the documents returns the following object:
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
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
相关文章推荐
- MongoDB 3.0 for java (一、java连接MongoDB)
- mongoDB 3.0 安全权限访问控制
- mongodb命令使用大全(常用命令)
- Linux下安装mongodb详细过程
- MongoDB Auto-Sharding(自动分片)入门介绍
- mongodb基础结构
- WINSERVER 2008 上安装MONGODB 64位
- Mac OS下安装MongoDB
- MongoDB : couldn’t open /data/db/yourdb.ns errno:13 Permission denied
- Hadoop 之Mong DB 之CentOS 6 使用 yum 安装MongoDB及服务器端配置
- 8天学通MongoDB——第一天 基础入门
- MongoDB的一些命令
- Mongodb安装 for linux
- Hadoop 之MongoDB
- Mongodb 请求处理流程
- Mongodb 请求处理流程
- Mongodb 请求处理流程
- mongodb连接池配置
- MongoDB在window和linux的安装
- MongDB的安装和基本操作 二(增删改查)