6.Insert Documents-官方文档摘录
总结
1.插入单文档
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
) db.inventory.find({item:"canvas"})
2.插入多文档
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
# 返回多行
db.inventory.find({})
This page provides examples of insert operations in MongoDB.
CREATING A COLLECTION
If the collection does not currently exist, insert operations will create the collection.
Insert a Single Document
New in version 3.2.
db.collection.insertOne() inserts a single document into a collection.
The following example inserts a new document into the inventory collection. If 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.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
You can run the operation in the web shell below:
insertOne() returns a document that includes the newly inserted document’s _id field value. For an example of a return document, see db.collection.insertOne() reference.
To retrieve the document that you just inserted, query the collection:
db.inventory.find( { item: "canvas" } )
Insert Multiple Documents
New in version 3.2.
db.collection.insertMany() can insert multiple documents into a collection. Pass an array of documents to the method.
The following example inserts three new documents into the inventory collection. If the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document. See Insert Behavior.
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
You can run the operation in the web shell below:
insertMany() returns a document that includes the newly inserted documents _id field values. See thereference for an example.
To retrieve the inserted documents, query the collection:
db.inventory.find( {} )
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 _idfield.
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
Write Acknowledgement
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.
6.Insert Documents-官方文档摘录的更多相关文章
- Cocos Creator 加载和切换场景(官方文档摘录)
Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...
- ng的概念层次(官方文档摘录)
官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...
- Cocos Creator 使用计时器(官方文档摘录)
在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...
- Cocos Creator 生命周期回调(官方文档摘录)
Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...
- angular 模板语法(官方文档摘录)
https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...
- Qt元类型(MetaType)注册入门(附一些官方文档的关键摘录)
昨天调试项目时,突然发现如下消息: QObject::connect: Cannot queue arguments of type 'ERROR_LEVEL' (Make sure 'ERROR_L ...
- iOS开发官方文档汇总
程序员的学习过程是无止境的,程序员学习的途径是多样的.可以从视频教程中领悟,也可以从他人的代码中 理解.但当我们专注于某一个平台在开发的时候,对于某个API使用或者功能实现有疑问,通常简单的测试可以让 ...
- TestNG官方文档中文版(2)-annotation(转)
1. 介绍 TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器). 编写一个测试的 ...
- Google Android官方文档进程与线程(Processes and Threads)翻译
android的多线程在开发中已经有使用过了,想再系统地学习一下,找到了android的官方文档,介绍进程与线程的介绍,试着翻译一下. 原文地址:http://developer.android.co ...
- 常用SQL_官方文档使用
SQL语句基础理论 SQL是操作和检索关系型数据库的标准语言,标准SQL语句可用于操作关系型数据库. 5大主要类型: ①DQL(Data Query Language,数据查询语言)语句,主要由于se ...
随机推荐
- C# 冒泡排序
class Program { static void swap( ref int atemp, ref int btemp)//注意ref的使用 { int temp = atemp; atemp ...
- Javascript网页截屏的方法
最近我在研究开发一个火狐插件,具体的功能是将网页内容截屏并分享到微博上.目前基本功能已经实现,大家可以在 @程序师视野 里看到用这个截图插件分享的微博的效果. 之前我曾写过如何将canvas图形转换成 ...
- linux学习笔记10---命令nl
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号! nl命令读取 file 参数(缺省情况下标准输入),计算输入中的行号,将计算过的行号写入标准输出.在输出中,n ...
- Android CoordinatorLayout 入门介绍
Android CoordinatorLayout 入门介绍 CoordinatorLayout View 知道如何表现 在 2015 年的 I/O 开发者大会上,Google 介绍了一个新的 And ...
- Quotations中页面弹出的问题
- SQLite学习手册(转)
原文网址:http://www.cnblogs.com/stephen-liu74/archive/2012/01/22/2328757.html 在实际的应用中,SQLite作为目前最为流行的开源嵌 ...
- git三种模式及常用命令
Git的三种重要模式,分别是已提交.已修改.已暂存. 已提交(committed):表示数据文件已经顺利提交到Git数据库中. 已修改(modified):表示数据文件已经被修改,但未被保存到Git数 ...
- java部署ubuntu后中文显示问号问题
1.首先先回忆自身项目的编码格式,即在本地进行编码时使用的编码格式.UTF-82.检测tomcat的设置问题,在web.xml和server中的设置:server.xml中: <Connecto ...
- CSS水平和垂直居中方案
我们在网页布局的时候,经常会碰到需要居中的情况,那下面就来讲一下有哪几种目前比较常用的居中方案,它们的优点和缺点分别又是什么. 一.水平居中 方法①:(父元素)text-align,(子元素)in ...
- python3----练习题(装饰器)
装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象.也就是说装饰器的作用就是为已经存在的对象添加额外的功能. 当使用 ...