MongoDB - Introduction to MongoDB, Databases and Collections
MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases.
Databases
In MongoDB, databases hold collections of documents.
To select a database to use, in the mongo shell, issue the use <db> statement, as in the following example:
use myDB
Create a Database
If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the mongo shell:
> use myNewDB
switched to db myNewDB
> db.myNewCollection1.insert( { x: 1 } )
...
WriteResult({ "nInserted" : 1 })
The insert() operation creates both the database myNewDB and the collection myNewCollection1 if they do not already exist.
For a list of restrictions on database names, see Naming Restrictions.
Collections
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.
Create a Collection
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
> db.myNewCollection2.insert( { x: 1 } )
WriteResult({ "nInserted" : 1 })
> db.myNewCollection3.createIndex( { y: 1 } )
2016-11-30T11:29:16.665+0800 I INDEX [conn6] build index on: myNewDB.myNewCollection3 properties: { v: 1, key: { y: 1.0 }, name: "y_1", ns: "myNewDB.myNewCollection3" }
2016-11-30T11:29:16.665+0800 I INDEX [conn6] building index using bulk method
2016-11-30T11:29:16.667+0800 I INDEX [conn6] build index done. scanned 0 total records. 0 secs
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>
Both the insert() and the createIndex() operations create their respective collection if they do not already exist.
For a list of restrictions on collection names, see Naming Restrictions.
Explicit Creation
MongoDB provides the db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.
To modify these collection options, see collMod.
Document Validation
New in version 3.2.
By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection.
Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations. See Document Validation for details.
Modifying Document Structure
To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, update the documents to the new structure.
MongoDB - Introduction to MongoDB, Databases and Collections的更多相关文章
- MongoDB - Introduction to MongoDB, Capped Collections
Overview Capped collections are fixed-size collections that support high-throughput operations that ...
- MongoDB - Introduction to MongoDB, Documents
MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, th ...
- MongoDB - Introduction to MongoDB, MongoDB Extended JSON
JSON can only represent a subset of the types supported by BSON. To preserve type information, Mongo ...
- MongoDB - Introduction to MongoDB
MongoDB is an open-source document database that provides high performance, high availability, and a ...
- MongoDB - Introduction to MongoDB, BSON Types
BSON is a binary serialization format used to store documents and make remote procedure calls in Mon ...
- mongoDB & Nodejs 访问mongoDB (一)
最近的毕设需要用到mongoDB数据库,又把它拿出来再学一学,下盘并不是很稳,所以做一些笔记,不然又忘啦. 安装 mongoDB & mongoVUE mongoDB: https://www ...
- mongoDB操作命令及mongoDB的helper
此项目已开源,开源地址是: http://mongodbhelper-csharp.googlecode.com/svn/trunk/ mongodb的helper using System; usi ...
- java操作mongodb & springboot整合mongodb
简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...
- mongodb基本命令,mongodb集群原理分析
mongodb基本命令,mongodb集群原理分析 集合: 1.集合没有固定数据格式. 2. 数据: 时间类型: Date() 当前时间(js时间) new Date() 格林尼治时间(object) ...
随机推荐
- 处理Git不能上传大于100M文件问题
记录一下自己工作遇到的问题,免得下次再遇到了还到处网上查资料解决. 自己的项目的版本控制用的是Git,代码仓库在github托管.项目里用到了百度导航SDK,由于百度导航SDK有了新版本,于是就更新到 ...
- 集合由量大接口派生来:Collection 和 Map
Collection - 存放单个数据 |-List 实现类:ArrayList(Vector) LinkedList Vector的子类Stack |-Set 实现类:HashSet Tree ...
- Longest Substring with At Most Two Distinct
Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...
- [OS] 内核态和用户态的区别
http://blog.csdn.net/fatsandwich/article/details/2131707# http://jakielong.iteye.com/blog/771663 当一个 ...
- 【算法】—— 1到n中减少了一个数,顺序被打乱,找出缺失的数
问题 有0-n这n+1个数,但是其中丢了一个数,请问如何找出丢了哪个数? 五种方法 1)用1+2+...+n减去当前输入数据的总和.时间复杂度:O(n) 空间复杂度:O(1) [容易溢出] 2)用12 ...
- 多示例学习 multiple instance learning (MIL)
多示例学习:包(bags) 和 示例 (instance). 包是由多个示例组成的,举个例子,在图像分类中,一张图片就是一个包,图片分割出的patches就是示例.在多示例学习中,包带有类别标签而示例 ...
- [cdqzds] Challenge4
描述 给一个长为N的数列,有M次操作,每次操作时以下三种之一: (1)修改数列中的一个数 (2)求数列中某连续一段所有数的两两乘积的和 mod 1000000007 (3)求数列中某连续一段所有相邻两 ...
- 【刷题】BZOJ 5418 [Noi2018]屠龙勇士
www.lydsy.com/JudgeOnline/upload/noi2018day2.pdf Solution 将攻击的式子列出来,\(atk \times x-p \times y=a_i\) ...
- BZOJ 3881: [Coci2015]Divljak
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 553 Solved: 176[Submit][Sta ...
- Alpha 冲刺 —— 十分之九
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 多次测试软件运行 学习OPENMP ...