Home Database Mysql Tutorial MongoDB笔记一之简介与入门【第一次编辑:排版】

MongoDB笔记一之简介与入门【第一次编辑:排版】

Jun 07, 2016 pm 05:40 PM
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()

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which version is generally used for mongodb? Which version is generally used for mongodb? Apr 07, 2024 pm 05:48 PM

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Experience sharing on implementing real-time recommendation system using MongoDB Experience sharing on implementing real-time recommendation system using MongoDB Nov 03, 2023 pm 04:37 PM

With the development of the Internet, people's lives are becoming more and more digital, and the demand for personalization is becoming stronger and stronger. In this era of information explosion, users are often faced with massive amounts of information and have no choice, so the importance of real-time recommendation systems has become increasingly prominent. This article will share the experience of using MongoDB to implement a real-time recommendation system, hoping to provide some inspiration and help to developers. 1. Introduction to MongoDB MongoDB is an open source NoSQL database known for its high performance, easy scalability and flexible data model. Compared to biography

Where is the database created by mongodb? Where is the database created by mongodb? Apr 07, 2024 pm 05:39 PM

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

What are the advantages of mongodb database What are the advantages of mongodb database Apr 07, 2024 pm 05:21 PM

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

What does mongodb mean? What does mongodb mean? Apr 07, 2024 pm 05:57 PM

MongoDB is a document-oriented, distributed database system used to store and manage large amounts of structured and unstructured data. Its core concepts include document storage and distribution, and its main features include dynamic schema, indexing, aggregation, map-reduce and replication. It is widely used in content management systems, e-commerce platforms, social media websites, IoT applications, and mobile application development.

Where are the mongodb database files? Where are the mongodb database files? Apr 07, 2024 pm 05:42 PM

The MongoDB database file is located in the MongoDB data directory, which is /data/db by default, which contains .bson (document data), ns (collection information), journal (write operation records), wiredTiger (data when using the WiredTiger storage engine ) and config (database configuration information) and other files.

How to open mongodb How to open mongodb Apr 07, 2024 pm 06:15 PM

On Linux/macOS: Create the data directory and start the "mongod" service. On Windows: Create the data directory and start the MongoDB service from Service Manager. In Docker: Run the "docker run" command. On other platforms: Please consult the MongoDB documentation. Verification method: Run the "mongo" command to connect and view the server version.

See all articles