Mongodb 简明教程

MongoDB - Overview

MongoDB 是一个跨平台文档导向数据库,它提供高性能、高可用性和简单可扩展性。MongoDB 在集合和文档的概念上工作。

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.

Database

数据库是集合的物理容器。每个数据库都会在文件系统上获取自己的一组文件。单个 MongoDB 服务器通常具有多个数据库。

Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection

集合是一组 MongoDB 文档。它相当于 RDBMS 表。集合存在于单个数据库中。集合不强制执行模式。集合中的文档可以有不同的字段。通常,集合中的所有文档都具有相似或相关用途。

Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.

Document

文档是一组键值对。文档具有动态模式。动态模式意味着同一集合中的文档不必具有相同的字段集或结构,并且集合文档中的常见字段可能保存不同类型的数据。

A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

下表显示了 RDBMS 术语与 MongoDB 之间的关系。

The following table shows the relationship of RDBMS terminology with MongoDB.

RDBMS

MongoDB

Database

Database

Table

Collection

Tuple/Row

Document

column

Field

Table Join

Embedded Documents

Primary Key

Primary Key (Default key _id provided by MongoDB itself)

Database Server and Client

mysqld/Oracle

mongod

mysql/sqlplus

Sample Document

以下示例显示了博客网站的文档结构,它只是一个逗号分隔的键值对。

Following example shows the document structure of a blog site, which is simply a comma separated key value pair.

{
   _id: ObjectId(7df78ad8902c)
   title: 'MongoDB Overview',
   description: 'MongoDB is no sql database',
   by: 'tutorials point',
   url: 'http://www.tutorialspoint.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100,
   comments: [
      {
         user:'user1',
         message: 'My first comment',
         dateCreated: new Date(2011,1,20,2,15),
         like: 0
      },
      {
         user:'user2',
         message: 'My second comments',
         dateCreated: new Date(2011,1,25,7,45),
         like: 5
      }
   ]
}

_id 是一个 12 字节十六进制数字,可确保每份文档的唯一性。你可以在插入文档时提供 _id。如果你不提供,则 MongoDB 会为每份文档提供一个唯一 ID。这 12 个字节的前 4 个字节用于当前时间戳,接下来的 3 个字节用于机器 ID,接下来的 2 个字节用于 MongoDB 服务器的进程 ID,其余 3 个字节是简单的增量 VALUE。

_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. If you don’t provide then MongoDB provides a unique id for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of MongoDB server and remaining 3 bytes are simple incremental VALUE.