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) ...
随机推荐
- 6/4 sprint2 看板和燃尽图的更新
- Beta阶段——3
一.提供当天站立式会议照片一张: 二. 每个人的工作 (有work item 的ID) (1) 昨天已完成的工作: 今天主要是对管理员功能进行改进,解决了Alpha阶段出现的一些问题 (2) 今天计划 ...
- 课堂作业:alpha发布点评
1. 新蜂 项目:游戏俄罗斯方块 基础玩法已实现,部分功能尚不完备,总的来说已经很出色了.不过希望下降加速那部分可以优化一下,不要按住↓加速,而是按一下就使当前方块加速下落至底部就好了. 2. 天 ...
- Mac配置环境变量
Mac配置环境变量,以ant和maven为例 1.编辑 .bash_profile文件 cd vi .bash_profile 编辑内容为 ------------------------- ...
- Pytest - 进阶功能fixture
1. 概述 Pytest的fixture功能灵活好用,支持参数设置,便于进行多用例测试,简单便捷,颇有pythonic.如果要深入学习pytest,必学fixture. fixture函数的作用: 完 ...
- java的日志知识
java常用的日志有以下几种 : 一.jdk自带的java.util.logging包下的日志功能, 不常用. 二.commons-logging + log4j 的搭配 .log4j是日志功能的具 ...
- BZOJ5308 ZJOI2018胖
贝尔福特曼(?)的方式相当于每次将所有与源点直接相连的点的影响区域向两边各扩展一格.显然每个点在过程中最多更新其他点一次且这些点构成一段连续区间.这个东西二分st表查一下就可以了.注意某一轮中两点都更 ...
- 左连接,右连接和等值连接(left join,right join和inner join)
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- debian修改默认编辑器
刚才在一台机器上打开 crontab -e,跳出来的编辑器是nano,太难使... 在debian下是使用 update-alternatives 命令修改默认编辑器. 先查看一下使用帮助 # upd ...
- java多线程 --ConcurrentLinkedQueue 非阻塞 线程安全队列
ConcurrentLinkedQueue是一个基于链接节点的无界线程安全队列,它采用先进先出的规则对节点进行排序,当我们添加一个元素的时候,它会添加到队列的尾部:当我们获取一个元素时,它会返回队列头 ...