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的更多相关文章

  1. MongoDB - Introduction to MongoDB, Capped Collections

    Overview Capped collections are fixed-size collections that support high-throughput operations that ...

  2. MongoDB - Introduction to MongoDB, Documents

    MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, th ...

  3. MongoDB - Introduction to MongoDB, MongoDB Extended JSON

    JSON can only represent a subset of the types supported by BSON. To preserve type information, Mongo ...

  4. MongoDB - Introduction to MongoDB

    MongoDB is an open-source document database that provides high performance, high availability, and a ...

  5. MongoDB - Introduction to MongoDB, BSON Types

    BSON is a binary serialization format used to store documents and make remote procedure calls in Mon ...

  6. mongoDB & Nodejs 访问mongoDB (一)

    最近的毕设需要用到mongoDB数据库,又把它拿出来再学一学,下盘并不是很稳,所以做一些笔记,不然又忘啦. 安装 mongoDB & mongoVUE mongoDB: https://www ...

  7. mongoDB操作命令及mongoDB的helper

    此项目已开源,开源地址是: http://mongodbhelper-csharp.googlecode.com/svn/trunk/ mongodb的helper using System; usi ...

  8. java操作mongodb & springboot整合mongodb

    简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...

  9. mongodb基本命令,mongodb集群原理分析

    mongodb基本命令,mongodb集群原理分析 集合: 1.集合没有固定数据格式. 2. 数据: 时间类型: Date() 当前时间(js时间) new Date() 格林尼治时间(object) ...

随机推荐

  1. Internet History, Technology and Security (Week5.1)

    Week5 The Transport layer is built on the Internetwork layer and is what makes our network connectio ...

  2. 读《构建之法》一、二、十六章随笔a

    第一章    概论 “软件团队要从需求分析开始,把合适的需求梳理出来,然后逐步开展后续工作”:——p3 问题:好的用户体验要从软件分析开始,那么软件分析仅仅是从用户的需求出发吗? 我的看法:需求分析是 ...

  3. 数据结构复习笔记(ADT栈/LIFO表)

    栈是一种特殊的表,只在表首进行插入和删除操作,表首称之为栈顶,表尾称为栈底:栈的核心原则是先进后出,简称Last In First Out(LIFO表):常用的运算有:1.是否为空栈判断:2.栈是否满 ...

  4. Java自学基础用法

    在慕课上面简单学习了一下java语言的用法 简单的用法总结记录一下. 代码(学习输入,输出): package hello; import java.util.Scanner; public clas ...

  5. C1WPF制作OLAP Cube浏览工具

    经过前期一段时间对WPF的学习了解,相信大家对WPF有了一定的了解.今天我们一起来了解使用Component One(简称C1)的WPF控件制作CUBE浏览工具.其实这个OLAP控件官方已经有了很详细 ...

  6. 学习laravel源码之中间件原理

    刨析laravel源码之中间件原理 在看了laravel关于中间件的源码和参考了相关的书籍之后,写了一个比较简陋的管道和闭包实现,代码比较简单,但是却不好理解所以还是需要多写多思考才能想明白其中的意义 ...

  7. SpringBoot(八)_springboot集成swagger2

    swagger是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试. (1) 引入依赖,我们选择现在最新的版本 <dependency> &l ...

  8. Qt——数据库编程

    一.概述 Qt提供了一个类似JDBC的数据库接口,需要为每个可以连接的特定数据库提供驱动程序,可以通过 QStringList QSqlDatabase::drivers() 知道当前版本的Qt哪些驱 ...

  9. MT【160】格点

    (2017年清华大学  THUSSAT) 把不超过实数 $x$ 最大整数记为 $[x]$,任取互质且不小于 3 的正奇数 $m,n$,令$$I=\sum_{i=1}^{\frac{m-1}{2}}\l ...

  10. 解题:SCOI 2005 骑士精神

    题面 我把这个当做IDA*的模板题的说,说说我个人对IDA*的理解 IDA*是一个DFS,和A*一样,它也有一个乐观的估价函数.这里这个估价函数是用来限制状态的扩展的,如果当前代价加上乐观的估计都无法 ...