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) ...
随机推荐
- 批量梯度下降(BGD)、随机梯度下降(SGD)以及小批量梯度下降(MBGD)的理解
梯度下降法作为机器学习中较常使用的优化算法,其有着三种不同的形式:批量梯度下降(Batch Gradient Descent).随机梯度下降(Stochastic Gradient Descent ...
- 车牌识别算法库EasyPR的使用
主要参考以下两个博客: http://blog.csdn.net/junmuzi/article/details/49888123 http://blog.csdn.net/Lucas66666/ar ...
- Java 经典 书籍
1.<你的灯还亮着么> 方法论 2.<程序员修炼之道 从小工到专家> 方法论 3.<发布!软件的设计与部署> 案例&经验总结 4.<思考,快与慢> ...
- datatables 排序 如何禁止
$.extend( true, $.fn.dataTable.defaults, { "searching": false, "ordering" ...
- selenium之数据驱动框架应用WPS个人中心自动签到
wps在注册后,有个每日签到的功能,签到后有几率送wps的专属金币[稻米],为了免费获得,又不想每天都是人工去执行签到动作,所以用selenium写了个小脚本,准备用数据驱动框架来完成这个事情,数据驱 ...
- MiniUI框架合并单元格
在项目中遇到合并单元格的问题,所以总结一下. 用的是miniUI框架,所以只谈miniUI中的单元格合并. (1)必须添加onLoad="onLoad" (2)需要在JS中进行单元 ...
- sql中详解round(),floor(),ceiling()函数的用法和区别?
round() 遵循四舍五入把原值转化为指定小数位数,如:round(1.45,0) = 1;round(1.55,0)=2floor()向下舍入为指定小数位数 如:floor(1.45,0)= 1; ...
- JS 随机整数
<script> function GetRandomNum(Min,Max){ var Range = Max - Min; var Rand = Math.random() ...
- P2234 [HNOI2002]营业额统计
题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
- MT【147】又见最大最小
(2018浙江省赛12题)设$a\in R$,且对任意的实数$b$均有$\max\limits_{x\in[0,1]}|x^2+ax+b|\ge1$求$a$的范围_____解答:由题意$\min\li ...