mongodb概述
MongoDB consists of a set of databases. Each database again consists of Collections. Data in MongoDB is stored in collections. The below figure depicts the typical database structure in MongoDB.
MongoDB由一组数据库组成。 每个数据库又由集合组成。 MongoDB中的数据存储在集合中。 下图描述了MongoDB中的典型数据库结构。
(Database in MongoDB)
Database in MongoDB is nothing but a container for collections. We will learn how to create a new Database, drop a Database and how to use an existing Database in the coming lessons.
MongoDB中的数据库不过是集合的容器。 在接下来的课程中,我们将学习如何创建新数据库,删除数据库以及如何使用现有数据库。
(Collections in MongoDB)
- Collection is nothing but a set of MongoDB documents. These documents are equivalent to the row of data in tables in RDBMS. But, collections in MongoDB do not relate to any set schema as compared to RDBMS. Collections are a way of storing related data. Being schemaless, any type of Document can be saved in a collection, although similarity is recommended for index efficiency. Document's can have a maximum size of 4MB.
集合不过是一组MongoDB文档而已。 这些文档等效于RDBMS中表中的数据行。 但是,与RDBMS相比,MongoDB中的集合与任何集合模式都不相关。 集合是一种存储相关数据的方法。 没有架构,任何类型的文档都可以保存在集合中,尽管建议使用相似性以提高索引效率。 文件的最大大小为4MB。 - We can use namespace to logically group and nest collections. For example : There can be one collection named
db.studytonight.users
to save user informations, then there can be others likedb.studytonight.forum.questions
anddb.studytonight.forum.answers
to store forum questions and answers respectively.
命名空间对集合进行逻辑分组和嵌套。 例如:可以有一个名为db.studytonight.users
集合来保存用户信息,然后可以有诸如db.studytonight.forum.questions
和db.studytonight.forum.answers
类的db.studytonight.forum.answers
来分别存储论坛问题和答案。 - If we create an Index
索引 ,则该索引将仅适用于该命名空间。 我们将在本教程的后面部分学习如何创建索引。 - A collection is physically created as soon as the first document is created in it.
在第一个文档中创建集合后,便会立即创建一个集合。 - You must be wondering why create multiple collections with different namespace, when we can keep any form or data in a single collection itself. It's because, MongoDB does not index attributes for totally unrelated documents. So it is advised to keep related data in collections.
您一定想知道为什么在我们可以将任何形式或数据保留在单个集合本身中的情况下,为什么创建具有不同名称空间的多个集合。 这是因为,MongoDB不会为完全不相关的文档建立索引属性。 因此建议将相关数据保留在集合中。
(Document in MongoDB)
Document in MongoDB is nothing but the set of key-value pairs. These documents will have dynamic schema which means that the documents in the same collection do not need to possess the same set of fields.
MongoDB中的文档不过是一组键值对。 这些文档将具有动态模式,这意味着同一集合中的文档不需要拥有相同的字段集。
Since MongoDB is considered as a schema-less database, each collection can hold different type of objects. Every object in a collection is known as Document, which is represented in a JSON like (JavaScript Object Notation) structure(nothing but a list of key-value pair). Data is stored and queried in BSON, its binary representation of JSON-like data. You can know more about BSON format from this webpage → BSON Specification and BSON Types
BSON中存储和查询, BSON是类似JSON数据的二进制表示形式。 您可以从此页面了解有关BSON格式的更多信息→ BSON规范和BSON类型
(Sample Data in MongoDB)
Note: In the above figure, the field _id
represents the primary key identifier of the given document. MongoDB also stores the values in the form of arrays of value as well. Infact any type of data can be stored as values, and nothing requires to be pre-defined. In other words, you do not have to predefine the type of data to be stored, you can store anything you want. Remember, MongoDB is schema-less.
注意:在上图中,字段_id
代表给定文档的主键标识符。 MongoDB还以值数组的形式存储值。 实际上,任何类型的数据都可以存储为值,并且不需要预先定义任何内容。 换句话说,您不必预先定义要存储的数据类型,就可以存储所需的任何数据。 记住,MongoDB是无架构的。
Documents are not identified by a simple ID, but by an object identifier type. The default Id is a combination of machine identifier, timestamp and process id to keep it unique, but user can changes it to anything.
文档不是通过简单的ID来标识的,而是通过对象标识符的类型来标识的。 默认ID是机器标识符,时间戳和进程ID的组合,以使其保持唯一,但用户可以将其更改为任何值。
(Example of Array as value)
{ _id : 112233,
name : "Viraj",
education : [
{
year : 2029,
course : "BTECH",
college : "IIT, Delhi"
},
{
year : 2031,
course : "MS",
college : "Harvard College"
}
]
}
(Example of Different Datatypes in one Document)
The value of fields in a document can be anything, including other documents, arrays, and arrays of documents, date object, a String etc.
文档中字段的值可以是任何值,包括其他文档,数组和文档数组,日期对象,字符串等。
var mydoc = {
_id : ObjectId("5099803df3f4948bd2f98391"),
name : { first: "Alan", last: "Turing" },
birth : new Date('Jun 23, 1912'),
death : new Date('Jun 07, 1954'),
contribs : [ "Turing machine", "Turing test", "Turingery" ],
view : NumberLong(1250000)
}