`
lobbychmd
  • 浏览: 8902 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
文章分类
社区版块
存档分类
阅读更多

 

 

首先说一下这个翻译主要是给自己看的,并非很完全,错误难免,翻译只求达意。

 

 

 

 

 

 

 

 


MongoDB
强大而易于上手.这章将介绍它的一些基本概念.

一个 document mongoDB 的基本管理单元,可简单认为跟关系数据库的一个行差不多,但表现力更强些

类似的,collection 相当于表(无元数据信息)
.
• MongoDB
的一个实例可以操纵几个 database,每个都允许单独的权限设置和
Collection
• MongoDB
自带了一个强大的 javascript 语法的shell,可以进行数据库管理
.
每个 document 有一个特殊的 key,  "_id", collection 里面它是唯一的。

 

 

 

 

 

Documents

 

 

MongoDB 的中心是document 的概念:多个key/Value 的有序集合。不同的编程语言对这个结构有差异,但大多数都有一个相当合适的类型来表达,例如maphashdictionary,在Javascript里面的例子如下

{"greeting" : "Hello, world!"}

 

 

这个简单的文档包括一个keygreeting,有一个值叫做 “hello world”,大多数文档更复杂些,包括几个key/value 的对

{"greeting" : "Hello, world!", "foo" : 3}


 

这个例子是几个重要概念的很好说明

Key/value 对在 documents 中是排序的, 前面的document 跟下面这个document 是不同的:

{"foo" : 3, "greeting" : "Hello, world!"}

 

 

 

 

大多数情况这个顺序并不重要。实际上一些编程语言例如python dictionary perl bashes 并没有维护排序。Drivers for those lan- guages usually have some mechanism  for specifying documents with ordering for the rare cases when it is necessary. (Those cases will be noted throughout the text.)

 

documents value 并不是仅仅类似 “blobs.” 它们包含几种数据类型   (或者嵌套的文档). 这个例子中 "greeting" 的值是一个字符串, "foo" 的值是整数.

 

Key 必须是String. 任意utf-8的字符都能用做key,除非:

key不能包括\n.这个字符代表了key 的结尾.

  . $ characters  have some special properties  and should  be used only in certain circumstances, as described  in later chapters.  In general, they should  be considered reserved, and drivers will complain if they are used inappropriately.

 最好不用_开头,虽然没有严格限制.

 

MongoDB  是类型敏感和大小写敏感的,例如下面的document 是不同的 :

{"foo" : 3}

{"foo" : "3"}

 

下面两个也不同:

{"foo" : 3}

{"Foo" : 3}

 

 

最后有一个重要的事情是MongoDB 不能包括重复key,例如

{"greeting" : "Hello, world!", "greeting" : "Hello, MongoDB!"}


Collections

 

Collection Document 的集合。如果ducoment 是关系数据库的行那么collection 就是表

 

Schema-Free

Collections 不需要描述结构。这意味这一个collection 里面的document 可以有不同的表现。例如,下面这两个可以放置同一个Collection:

{"greeting" : "Hello, world!"}

{"foo" : 5}

 

注意上面这两个不仅仅key 不一样,类型也不一样。如果是这样的话,那“还要分Collection 干嘛?”是一个好问题,下面是几个原因:

 

 不同类型的document放在一个collection将是开发者和管理员的噩梦。开发者需要确保每个查询只返回某个类型的documentor that the application code performing  a query can handle documents of different shapes。如果我们查询一个博客文章,就很难将作者从document 里面区分出来

 查询会快一些。例如,如果我们在collection 里面有一个typekey说明是摘要还是全文,合在一个collection里面查询比分开collection 查询更慢些。


 

   document 按同一类型存入一个collection有利于数据定位。从一个只包含文章的collection找文章,将比从一个既包含文章又包含作者的collection 更减少磁盘IO

.当我们创建索引时我们开始在文档中组织结构。(尤其唯一索引)。这些索引在每个collection 里面定义。一个collection 放同一类型document 的话,索引会更有效率

正如你所看到的,将document分组存放有明智的原因。Mongodb 只是放松了限制,允许开发者自己掌握复杂性而已。


Naming

 

 

Subcollections

一个组织Collection 的约定是用在命名空间里面.来划分SubCollection。例如blog blog.post ,这只是一个组织的目的,在blog 这个collection blog.post 并没有任何联系。

 

虽然subcollection 并没有其他特别的含义,但在一些工具里面很有用

 

GridFS, 一个存储大文件的协议,用subcollection来存储文件的元信息,.

The  MongoDB  web  console   organizes  the  data   in  its  DBTOP  section   by subcollection  (see Chapter  8 for more information on administration).

大多数驱动提供语法糖来存储一个collection subcollection。例database shelldb.blog 将提供blog collectionDb.blog.post 提供blog.posts collection

 

 

Subcollections  mongodb组织数据的好方法,推荐!

 

 

Databases

除了用collection分组documentmongodb database 分组collection。一个mongodb实例能主管几个database,它们之间是完全独立的,也有独立的权限,每个database也是用不同的磁盘文件来存储。通常一个应用程序用1database,几个应用程序可以合用一个server

 

类似collections, databases 也是用name 来标示。命名规则:

 

The empty string ("") is not a valid database name.

A database name cannot contain any of these characters: ' ' (a single space), ., $, /,

\, or \0 (the null character).

Database names should be all lowercase.

Database names are limited to a maximum  of 64 bytes.

 

要记得database 的名字的一件事是它们最终是文件系统的实际文件。这可以解释为什么 This explains why many of the previous restrictions exist in the first place.

 

database names也有几个保留字。通过特殊的语义你可以访问它们

 

admin

根数据库。用在认证方面用到。如果一个用户加入到admin数据库就自动获得所有数据库的权限。有一些全局的命令,例如列出所有database 的命令,只能在admin 数据库执行。

local

这个数据库将永远不会被复制,并可以用来存储任何collection that should be local to a single server (see Chapter  9 for more information about rep- lication and the local database).

config

When Mongo is being used in a sharded setup (see Chapter 10), the config database is used internally to store information about the shards.

 

在考虑collection 和它的包含者的名字的时候,你将得到一个完整的命名空间。例如如果你再cms数据库用blog.posts 集合,这个集合的命名空间就是cms.blog.posts. 命名空间限制为121个字符。实际上最好100个以内。更多信息请看附录c  
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics