MongoDB笔记一之简介与入门【第一次编辑:排版】
煮酒品茶:大量序语和实例引用自书中,品茶对其实验后做的笔记上载至博客,任何有版权的人都可以直接下架下文章,谢谢合作。2012/8/1414:00MongoDB笔记一之简介
煮酒品茶:大量序语和实例引用自书中,品茶对其实验后做的笔记上载至博客,任何有版权的人都可以直接下架下文章,谢谢合作。
2012/8/14 14:00 MongoDB笔记一之简介与入门【第一次编辑:排版】
elain2012写的MongoDB 实战系列,真心不错!
MongoDB权威指南
第一章
MongoDB是一种强大、灵活、可扩展的数据存储方式。它扩展了关系型数据库的众多有用功能,免备案空间,如辅助索引、范围查询和排序。MongoDB的功能非常丰富,比如内置的对MapReduce式聚合的支持,以及对地理空间索引的支持。
优点:
1、丰富的数据模型
MongoDB是面向文档的数据库,将原来的“行”(row),的概念换成更加灵活的“文档”(document)模型。
MongoDB没有模式:文档的键不会事先定义也不会固定不变。
2、容易扩展
采用的面向文档的数据模型使其可以自动在多台服务器之间分割数据,还可以平衡集群的数据和负载,自动重排文档,开发者可以专注于编写应用,需要更大的容量,只需在集群中添加新机器,然后让数据库来处理剩下的事。
3、丰富的功能
索引(多种快速查询,也提供了咱一的,复合的和地理空间索引能力。)
存储JavaScript(开发人员不必使用存储过程了,可以直接在服务端存储JavaScript的函数和值)
聚合(支持MapReduce和其它聚合工具)
固定集合(集合的大小是有上限的,对某些类型的数据【如:日志】特别有用)
文件存储(支持用一种容易使用的协议存储大型文件和文件的元数据)
4、不牺牲速度
5、简便的管理
主从复制,副本集,分片等。
第二章 入门
基本概念
----------------------------------------
文档是MongoDB中数据的基本单元。(doucument)
集合可以被看做是没有模式的表(collections)
MongoDB的单个实例可以容纳多个独立的数据库,每一个都有自己的集合和权限。
MongoDB自带JavaScript shell,用于管理MongoDB实例和操和数据。
每一个文档都有一个特殊的键"_id",它在文档所处的集合中是咱一的。
-----------------------------------------
文档:多个键及其关联的值有序的放置在一起便是文档。(key:vlaue)
{"cwtea" : "hello","key":"vlaue"}
注:文档中键的顺序并不重要。
1、文档中的键/值对应是有序的
{"cwtea" : "hello","key":"vlaue"}和{"key":"vlaue",网站空间,"cwtea" : "hello"}完全不同
2、文档中的值可以是多种数据类型,整字,字符串,甚至文档。键可以使用任意UTF-8字符。
3、键不能含有\0(空字符),这个字符用来表示键的结尾。
4、.和$有特别的意义,只有在特定环境下才能使用。
5、以下划线"_"开头的键是保留的,香港服务器,虽然这个并不是严格要求的。
6、mongoDB区分类型和区分大小写
{"foo" : 3} 不同于{"foo" : "3"} 整数和字符串
{ "foo" : "3"}不同于{"Foo" : "3"} 大小写
7、mongoDB不能有重复的键
集合
概念:集合就是一组文档 collections
无模式
概念:不同文档可存在同一个集合中。
命名:
可以满足下列条件的任意UTF-8字符串。
1、集合名不能是空字符串""
2、集合名不能含 有\0字符(空字符),这个字符表示集合名的结尾.
3、集合名不能以"system."开发,这是为系统集合保留的前缀。
4、用户创建的集合名字不能含有保留字符$。有些驱动程序的确支持在集合名里面包含$,这是因为某些系统生成的集合中包含该字符。
子集合:
组织集合的一种惯例是使用 "." 字符分开的按命名空间划分的子集合。
GridFS是一种存储大文件的协议,使用子集合来存储文件的元数据,这样就与内容 分开了。
Web控制台通过子集合的方式将数据组织在DBTOP部分
绝大多数驱动程序都提供语法糖,为访问指定集合的子集合提供方便。
(db.blog代表blog集合,db.blog.posts代表blog.posts集合)
数据库:多个集合组成数据库。
一个MongoDB实例可承载多个数据库,它们之间可视为完全独立,每个数据库都有独立的权限控制,即便在磁盘上,不同的数据库存也放在不同的文件中。
命名:数据库名最终会变成文件系统里的文件,所以会有以下限制。
不能是空字符串("")
不得含有''(空格)、,、$、/、/和\0(空字符)
就全部是小写
最多64字节
保留的数据库名:
admin(是"root"数据库,添加一个用户到这个数据库,则具有最高权限)
local(这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合)
config(当Mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息)
命名空间:把数据库的名字放到集合名前面,得到就是集合的完全限定名db.blog.posts,命名空间的长度不得超过121,实际使用最好不超过100字节。
MongoDB shell
自带一个javascript shell,可以从命令行与MongoDB实例交互,可以执行管理操作,检查运行实例,变或做其他尝试。可以做javascript所做之事。
> math.sin(math.pi /2)
Mon Aug 13 23:28:19 ReferenceError: math is not defined (shell):1
> Math.sin(Math.PI /2)
1
> new Date("2010/1/1")
ISODate("2009-12-31T16:00:00Z")
> new Date("2010/1/1");
ISODate("2009-12-31T16:00:00Z")
> "hello ,world!".replace("world", "mongodb")
hello ,mongodb!
> "hello ,world!".replace("world1", "mongodb")
hello ,world!
> "hello ,2".replace("2", "mongodb")
hello ,mongodb
> function factorial (n) {
... if (n
... return n * factorial(n-1)
... }
> factorial(5);
120
shell的基本操作:
创建、读取、更新和删除。
创建:
> use blog
switched to db blog
> post = {"title" : "my blog post",
... "content" : "here is my blog post.",
... "date" : new Date()}
{
"title" : "my blog post",
"content" : "here is my blog post.",
"date" : ISODate("2012-08-13T16:24:51.467Z")
}
> db.blog.insert(post)
> db.blog.find()

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports
