MongoDB - MongoDB CRUD Operations, Insert Documents
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 name, age, 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 name, age, 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 name, age, 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:
- db.collection.update() when used with the upsert: true option.
- db.collection.updateOne() when used with the upsert: true option.
- db.collection.updateMany() when used with the upsert: true option.
- db.collection.findAndModify() when used with the upsert: true option.
- db.collection.findOneAndUpdate() when used with the upsert: true option.
- db.collection.findOneAndReplace() when used with the upsert: true option.
- db.collection.save().
- db.collection.bulkWrite().
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.
MongoDB - MongoDB CRUD Operations, Insert Documents的更多相关文章
- MongoDB - MongoDB CRUD Operations, Delete Documents
Delete Methods MongoDB provides the following methods to delete documents of a collection: Method De ...
- MongoDB - MongoDB CRUD Operations, Update Documents
Update Methods MongoDB provides the following methods for updating documents in a collection: Method ...
- MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell
The db.collection.find() method returns a cursor. To access the documents, you need to iterate the c ...
- MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields
Different query operators in MongoDB treat null values differently. The examples on this page use th ...
- MongoDB - MongoDB CRUD Operations, Query Documents, Project Fields to Return from Query
By default, queries in MongoDB return all fields in matching documents. To limit the amount of data ...
- MongoDB - MongoDB CRUD Operations, Query Documents
Query Method MongoDB provides the db.collection.find() method to read documents from a collection. T ...
- MongoDB - MongoDB CRUD Operations
CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...
- MongoDB - MongoDB CRUD Operations, Bulk Write Operations
Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...
- Mongodb系列- CRUD操作介绍
---恢复内容开始--- 一 Create 操作 在MongoDB中,插入操作的目标是一个集合. MongoDB中的所有写入操作在单个文档的层次上都是原子的. For examples, see In ...
随机推荐
- Android开发随笔2
昨天:对anroid的系统架构了解比如:基于linux内核,整合库函数和java编译器并且为上层提供封装好的api和一些基本系统级应用 创建一个安卓的模拟器 了解了ddms的作用和内容 利用已有的工具 ...
- 团队作业the end
GitHub 王德钊 李镇平 陈启昌 黄益颂 汪倍民 演示动态图:为避免现场演示翻车,各个团队需要给出项目各个功能点运行的git图. 还在肝: 分工协作:团队的成员如何分工协作的?对项目的贡献如何(用 ...
- 软工1816 · Alpha冲刺(5/10)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 后端代码复审 福大各个食堂的菜品口味量化.属性标记 组织前后端线下协作 接下来 ...
- net项目调试时,读取主干或其他项目代码问题
最近调试项目的时候出了一个很奇怪的问题. 项目总是去读取另外一个项目的配置文件,甚至执行的代码都是另外一个项目的. 我用vs修改代码时候,甚至修改到了其他项目的代码,生成不报错,很奇怪. 本来怀疑是v ...
- TCP系列45—拥塞控制—8、SACK关闭的拥塞撤销与虚假快速重传
一.概述 这篇文章介绍一下TCP从Recovery状态恢复到Open状态的时候cwnd的更新.我们在tcp重传部分的文章中曾经介绍过虚假重传的概念,Linux在探测到虚假重传的时候就会执行拥塞撤销操作 ...
- CentOS安装crontab及使用方法(转)
CentOS安装crontab及使用方法(转) 安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum ins ...
- windows安装安卓开发环境Eclipse+SDK+ADT
准备条件 操作系统:win7 64位 需要的资源:JDK.Eclipse.SDK.ADT 步骤简介: 第一步:下载安装最新版JDK 第二步:下载安装Eclipse 第三步:下载安装SDK 第四步:安装 ...
- 第78天:jQuery事件总结(一)
jQuery事件总结(一) 现在就一点一点积累自己的知识体系,记录自己学到的和自己所理解的jQuery. JavaScript和HTML之间的交互式通过用户和浏览器操作页面时引发的事件机制来处理 ...
- 【.Net+数据库】Unable to convert MySQL date/time value to System.DateTime
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
- [十五]SpringBoot 之 启动加载数据
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...