MongoDB concept Attentions

1. Every document has a special key, "_id",  that  is unique across  the document's collection.

2. Key/value pairs in documents are ordered.
{"foo" : 3, "greeting" : "Hello, world!"} 
{"greeting" : "Hello, world!", "foo" : 3}
这两条document不相同.

3. The keys in a document are strings. Any UTF-8 character is allowed in a key, with a
few notable exceptions:
3.1 Keys must not contain the character \0 (the null character). This character is used
to signify the end of a key.
3.2 The . and $ 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.
3.3 Keys starting with _ should be considered reserved; although  this  is not strictly
enforced.
3.4 MongoDB is type-sensitive and case-sensitive. For example, these documents are
distinct:
{"foo" : 3}
{"foo" : "3"}
As are as these:
{"foo" : 3}
{"Foo" : 3}

4. A final important thing to note is that documents in MongoDB cannot contain duplicate
keys. For example, the following is not a legal document:
{"greeting" : "Hello, world!", "greeting" : "Hello, MongoDB!"}

5. Collections are schema-free. This means that the documents within a single collection
can have any number of different “shapes.” 
For example, both of the following documents could be stored in a single collection:
{"greeting" : "Hello, world!"}
{"foo" : 5}

6. Note that the previous documents not only have different types for their values (string
versus integer) but also have entirely different keys. Because any document can be put
into any collection, the question often arises: “Why do we need separate collections at
all?” It’s a good question—with no need  for separate schemas  for different kinds of
documents, why  should we  use more  than  one  collection?  There  are  several  good
reasons:
6.1 Keeping different kinds of documents in the same collection can be a nightmare
for developers and admins. Developers need to make sure that each query is only
returning documents of a certain kind or that the application code performing a
query can handle documents of different shapes. If we’re querying for blog posts,
it’s a hassle to weed out documents containing author data.
6.2 It is much faster to get a list of collections than to extract a list of the types in a
collection. For example, if we had a type key in the collection that said whether
each document was a “skim,” “whole,” or “chunky monkey” document, it would
be much slower to find those three values in a single collection than to have three
separate collections and query for their names (see  “Subcollections”
on page 8).
6.3 Grouping documents of the same kind together in the same collection allows for
data locality. Getting several blog posts from a collection containing only posts will
likely require fewer disk seeks than getting the same posts from a collection con-
taining posts and author data.
6.4 We begin to impose some structure on our documents when we create indexes.
(This is especially true in the case of unique indexes.) These indexes are defined
per collection. By putting only documents of a single type into the same collection,
we can index our collections more efficiently.
  As you can see, there are sound reasons for creating a schema and for grouping related
types of documents together. MongoDB just relaxes this requirement and allows de-
velopers more flexibility.

7. Naming
A collection is identified by its name. Collection names can be any UTF-8 string, with
a few restrictions:
7.1 The empty string ("") is not a valid collection name.
7.2 Collection names may not contain the character \0 (the null character) because
this delineates the end of a collection name.
7.3 You should not create any collections that start with system., a prefix reserved for
system collections. For example, the  system.users collection contains the database’s
users, and the system.namespaces collection contains information about all of the
database’s collections.
7.4 User-created collections should not contain the reserved character $ in the name.
The various drivers available for the database do support using $ in collection
names because some system-generated collections contain it. You should not use
$ in a name unless you are accessing one of these collections.

8. Subcollections
One convention for organizing collections is to use namespaced subcollections sepa-
rated by the . character. For example, an application containing a blog might have a
collection named blog.posts and a separate collection named blog.authors. This is for
organizational purposes only—there is no relationship between the blog collection (it
doesn’t even have to exist) and its “children.”
Although subcollections do not have any special properties, they are useful and incor-
porated into many MongoDB tools:
8.1 GridFS, a protocol for storing large files, uses subcollections to store file metadata
separately from content chunks 
8.2 The MongoDB web console organizes the data in its DBTOP section by
subcollection 
8.3 Most drivers provide some syntactic sugar for accessing a subcollection of a given
collection. For example, in the database shell, db.blog will give you the blog col-
lection, and db.blog.posts will give you the blog.posts collection.
  Subcollections are a great way to organize data in MongoDB, and their use is highly
recommended.
有点类似PostgreSQL中,表继承的概念.

9. Databases
In addition to grouping documents by collection, MongoDB groups collections  into
databases. A single instance of MongoDB can host several databases, each of which can
be thought of as completely independent. A database has its own permissions, and each
database is stored in separate files on disk. A good rule of thumb is to store all data for
a single application in the same database. Separate databases are useful when storing
data for several application or users on the same MongoDB server.
Like collections, databases are identified by name. Database names can be any UTF-8
string, with the following restrictions:
9.1 The empty string ("") is not a valid database name.
9.2 A database name cannot contain any of these characters: ' ' (a single space), ., $, /,
\, or \0 (the null character).
9.3 Database names should be all lowercase.
9.4 Database names are limited to a maximum of 64 bytes.
One thing to remember about database names is that they will actually end up as files
on your filesystem. This explains why many of the previous restrictions exist in the first
place.
9.5 There are also several reserved database names, which you can access directly but have
special semantics. These are as follows:
admin
This  is the “root” database,  in terms of authentication. If a user  is added to the
admin database, the user automatically inherits permissions for all databases.
There are also certain server-wide commands that can be run only from the ad-
min database, such as listing all of the databases or shutting down the server.
local
This database will never be replicated and can be used to store any collections that
should be local to a single server .
config
When Mongo is being used in a sharded setup , the config database
is used internally to store information about the shards.
By prepending a collection’s name with its containing database, you can get a fully
qualified collection name called a  namespace.  For  instance,  if  you  are  using  the
blog.posts collection in the cms database, the namespace of that collection would be
cms.blog.posts. Namespaces are limited to 121 bytes in length and, in practice, should
be less than 100 bytes long.

时间: 2024-11-08 17:23:56

MongoDB concept Attentions的相关文章

Performing Data Write Operations with MongoDB

Introduction This article discusses the steps involved in performing data write operations with MongoDB, focusing on the roles of the Journal and Oplog applications. Journal is a concept on the MongoDB storage engine layer while plog is a capped coll

比MongoDB领先一到两年 打造企业级NoSQL数据库

这几年来, NoSQL数据库凭借其易扩展.高性能.高可用.数据模型灵活等特色吸引到了大量新兴互联网公司的青睐,包括国内的淘宝.新浪.京东商城.360.搜狗等都已经在局部尝试NoSQL解决方案. 广州巨杉数据库是一家专注于新型NoSQL分布式数据库研发的创业公司,目前已经获得首轮天使投资,核心产品是SequoiaDB.巨杉数据库的创始团队成员多来自于IBM北美实验室,长期从事关系型数据库DB2的研发工作.SequoiaDB 1.3于2013年4月正式发布(最新版本为1.5),主要面向政府.电信.金

python操作mongodb根据

  本文实例讲述了python操作mongodb根据_id查询数据的实现方法.分享给大家供大家参考.具体分析如下: _id是mongodb自动生成的id,其类型为ObjectId,所以如果需要在python中通过_id查询,就需要转换类型 如果pymongo的版本号小于2.2,使用下面的语句导入ObjectId ? 1 from pymongo.objectid import ObjectId 如果pymongo的版本号大于2.2,则使用下面的语句 ? 1 from bson.objectid

MySQL和MongoDB设计实例对比

MySQL是关系型数据库中的明星,MongoDB是文档型数据库中的翘楚.下面通过一个设计实例对比一下二者:假设我们正在维护一个手机产品库,里面除了包含手机的名称,品牌等基本信息,还包含了待机时间,外观设计等参数信息,应该如何存取数据呢? 如果使用MySQL的话,应该如何存取数据呢? 如果使用MySQL话,手机的基本信息单独是一个表,另外由于不同手机的参数信息差异很大,所以还需要一个参数表来单独保存. CREATE TABLE IF NOT EXISTS `mobiles` (     `id` 

MongoDB · 特性分析 · MMAPv1 存储引擎原理

MongoDB 的 mongod 服务管理一个数据目录,可包含多个DB,每个DB的数据单独组织,本文主要介绍 MMAPv1 存储引擎的数据组织方式. Database 每个 Database(DB) 由一个.ns文件及若干个数据文件组成 $ll mydb.* -rw------- 1 ydzhang staff 67108864 7 4 14:05 mydb.0 -rw------- 1 ydzhang staff 16777216 7 4 14:05 mydb.ns 数据文件从0开始编号,依次

Windows下Mongodb安装及配置

参考:http://blog.csdn.net/mzbonnt/article/details/51461331#comments **欢迎加入疯狂源代码学习.QQ群127591054 工作中遇到了,所以学习下.** 这里装的是最新的 安装文件:mongodb-win32-x86_64-2008plus-ssl-3.4.6-signed.msi 电脑配置:win10 64位 mongodb的安装很简单,设置好安装路径后,一直Next直到安装结束. 最大的坑就是MongoDB服务的安装,下面具体说

mongodb WriteConcern

Java代码   http://www.reader8.cn/jiaocheng/20130826/1875289.html   Java代码       (1)不安全的操作 a)ERRORS_IGNORED:只是将消息发出,忽略一切错误,甚至网络错误也不理会. b)UNACKNOWLEDGED:将消息发送给socket即返回,不确保数据写入到数据库,因此发生网络错误时会有提示.默认值. (2)处于弱状态 a)ACKNOWLEDGED:确保已将数据写入到shard中的主节点中,这时如果有针对主节

MongoDB:副本集出现大量authenticate db日志

问题描述 MongoDB:副本集出现大量authenticate db日志 Sun Jan 11 00:15:06.082 [conn34] authenticate db: local { authenticate: 1, nonce: "b0d836d66e2ae743", user: "__system", key: "8c947498e52c2d06dedbef54493b2d59" } Sun Jan 11 00:15:08.987 [

Use mongodb 1.8.1's replicaSet with auth,journal,keyFile feature

MongoDB replicSet 1.8.1 产品部署推荐: 1. 文件系统加载时使用参数noatime 2. no VM PAGEs 3. 推荐使用逻辑卷,文件系统推荐ext4或xfs 4. 3个full nodes 或 2个full nodes+1个arbiter node (最好是奇数个物理服务器,否则仲裁会有问题,例如两台物理机,两个mongod进程,相互网络不通的话,任何一台都无法达到majority,因此都无法成为primary.那就是只读了.因此本例的物理服务器只有2台是不合理的