MongoDB provides the following methods for inserting documents into a collection:

This page provides examples of insert operations in the mongo shell.

Insert Behavior

Collection Creation

If the collection does not currently exist, insert operations will create the collection.

_id Field

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

This also applies to documents inserted through update operations with upsert: true.

Atomicity

All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions

db.collection.insertOne()

New in version 3.2.

db.collection.insertOne() inserts a single document into a collection.

The following example inserts a new document into the users collection. The new document has three fields nameage, and status. Since the document does not specify an _id field, MongoDB adds the _id field with an ObjectId value to the new document. See Insert Behavior.

db.users.insertOne(
{
name: "sue",
age: 19,
status: "P"
}
)

insertOne() will return a document providing the inserted document’s _id field. See the reference for an example.

To retrieve the document that you just inserted, query the collection:

For more information and examples, see db.collection.insertOne().

db.collection.insertMany()

New in version 3.2.

db.collection.insertMany() inserts multiple documents into a collection.

The following example inserts three new documents into the users collection. Each document has three fields nameage, and status. Since the documents do not specify an _id field, MongoDB adds the _idfield with an ObjectId value to each document. See Insert Behavior.

db.users.insertMany(
[
{ name: "bob", age: 42, status: "A", },
{ name: "ahn", age: 22, status: "A", },
{ name: "xi", age: 34, status: "D", }
]
)

insertMany() will return a document providing each inserted document’s _id field. See the reference for an example.

To retrieve the inserted documents, query the collection:

db.users.find()

For more information and examples, see db.collection.insertMany().

db.collection.insert()

db.collection.insert() inserts a single document or multiple documents into a collection. To insert a single document, pass a document to the method; to insert multiple documents, pass an array of documents to the method.

The following example inserts a new document into the users collection. The new document has three fields nameage, and status. Since the document does not specify an _id field, MongoDB adds the _id field with an ObjectId value to the document. See Insert Behavior.

db.users.insert(
{
name: "sue",
age: 19,
status: "P"
}
)

db.collection.insert returns a WriteResult object providing status information.

For example, a successful insert returns the following WriteResult object:

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.

The following example inserts multiple documents into the users collection. Since the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document. See Insert Behavior.

db.users.insert(
[
{ name: "bob", age: 42, status: "A", },
{ name: "ahn", age: 22, status: "A", },
{ name: "xi", age: 34, status: "D", }
]
)

The method returns a BulkWriteResult object with the status of the operation. A successful insert of the documents returns the following BulkWriteResult object:

BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

For more information and examples, see db.collection.insert().

Additional Methods

The following methods can also add new documents to a collection:

See the individual reference pages for the methods for more information and examples.

Write Acknowledgement

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

最新文章

  1. AES加密
  2. 【RobotFramework】Selenium2Library类库关键字使用说明
  3. MVC中获取模型属性的Range和StringLength验证特性设置
  4. CGI、FastCGI和PHP-FPM浅析
  5. java防止脚本注入,通过拦截器实现
  6. IOS之Foundation之探究学习Swift实用基础整理<一>
  7. c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
  8. sql字符串插入函数STUFF
  9. git 基本命令 (常用)
  10. SpringMVC+highstock实现曲线报表
  11. Cocos2d-x 2.1.5 简单动画
  12. 源代码版本控制工具TortoiseSVN,AnkhSVN最新版本下载地址
  13. 模拟点击a链接
  14. 读书笔记 effective c++ Item 47 使用traits class表示类型信息
  15. 更改电脑名称后, Cnario无法播放画面和声音, 开机后停留在桌面, Cnario Player软件界面的停止按钮为蓝色可选状态
  16. Centos7 + Python3.6 + Django + virtualenv + gunicorn + supervisor 环境配置详解
  17. ucml设置列表分页类型
  18. 写日志(log)
  19. Centos7 安装redis服务
  20. Redis学习第八课:Redis高级实用特性(二)

热门文章

  1. 第22章 软件安装:源码与Tarball
  2. HDFS shell命令行常见操作
  3. perf的采样模式和统计模式
  4. 图解Redux三大核心的关系
  5. Vue 定时执行函数
  6. 【uoj#21】[UR #1]缩进优化 数学
  7. Spring Boot系列教程六:日志输出配置log4j2
  8. BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】
  9. oracle中 trunc(),round(),ceil(),floor的使用
  10. Django summernote 富文本