MongoDB 快速入门

MongoDB简介:

    MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

特点:

它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:

  *面向集合存储,易存储对象类型的数据。

  *模式自由。

  *支持动态查询。

  *支持完全索引,包含内部对象。

  *支持查询。

  *支持复制和故障恢复。

  *使用高效的二进制数据存储,包括大型对象(如视频等)。

  *自动处理碎片,以支持云计算层次的扩展性。

  *支持RUBY,PYTHON,JAVA,C++,PHP,C#等多种语言。

  *文件存储格式为BSON(一种JSON的扩展)。

*可通过网络访问。

使用原理:

 所谓“面向集合”(Collection-Oriented),意思是数据被分组存储在数据集中,被称为一个集合(Collection)。每个集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定义任何模式(schema)。

  模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。

  存储在集合中的文档,被存储为键-值对的形式。键用于唯一标识一个文档,为字符串类型,而值则可以是各种复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized Document Format)

  

以上是 百度百科 关于MongoDB的介绍。

说了那么多,我们现在重点要掌握两个概念:面向集合,BSON。

面向集合:(Collection-Oriented)

   我们知道在面向关系数据库中数据库是由一张张表构成的,而在MongoDB中数据库是由集合构成的,集合就像一个容器,里面可以存放任何结构的数据,每一条数据都有唯一的标识符(由MongoDB在你插入一条数据的时候自动为这条数据生成)。

   BSON:(Binary Serialized Document Format)

使用过JSON的都应该对JSON那种简洁优雅的数据表示方式印象深刻,在MongoDB中存储数据的形式也是以JSON那种 键-值对的形式 存储。

例如{x:10,y:20}我们将这条文档插入到集合 demo中 db.demo.save({x:10,y:20}); 这条数据在MongoDB中的表示

我们可以看到在这条数据中多出一个字段 “_id”(前面讲了有MongoDB自动生成)。事实上每条数据插入的时候MongoDB都会这条数据自动生成唯一的”_id”。我们在下面的使用过程中会逐步了解这些概念

MongoDB的安装

   下载适合系统的版本http://www.mongodb.org/downloads

   下载后解压到文件夹:我这里指定的是D:\Servers\MongoDB\

   打开命令窗口转到该目录下新建一个用于存放数据的文件夹

然后转到bin目录下

MongoDB服务就启动了

现在我们使用客户端连上MongoDB

打开一个新窗口,同样转到bin目录下

我们就连上了MongoDB。

MongoDB的使用:

创建数据库

这里要说明的是MongoDB不会为你马上创建数据库,知道你插入第一条文档的时候。

下面开始使用MongoDB Shell(MongoDB官方说MongoDB全面支持JavaScript,所以JavaScript脚本可以直接在MongoDB上运行)。例如我们用一个函数 计算前n项和

我们在使用MongoDB进行数据操纵之前再次介绍一下几个重要的概念:

Dynamic Schema ("Schema Free")

MongoDB has databases, collections, and indexes much like a traditional RDBMS. In some cases (databases and collections) these objects can be implicitly created, however once created they exist in a system catalog (db.systems.collections, db.system.indexes).

Collections contain (BSON) documents. Within these documents are fields. In MongoDB there is no predefinition of fields (what would be columns in an RDBMS). There is no schema for fields within documents – the fields and their value datatypes can vary. Thus there is no notion of an "alter table" operation which adds a "column". In practice, it is highly common for a collection to have a homogenous structure across documents; however this is not a requirement. This flexibility means that schema migration and augmentation are very easy in practice - rarely will you need to write scripts which perform "alter table" type operations. In addition to making schema migration flexible, this facility makes iterative software development atop the database easier.

上面英文那么多 无非就是讲了 我们之前讲的两个概念 “面向集合”  “BSON”。动态模式(自由模式) 也就是说MOngoDB对我们插入的任何一条文档没有像关系数据里面有很多模式的限制。我们可以随便插入任何文档,只要符合BSON格式。

A few things to note :

  • We did not predefine the collection. The database creates it automatically on the first insert.(我们不需要预先定义集合,集合会在我们插入第一条文档的时候自动创建)
  • The documents we store can have different fields - in fact in this example, the documents have no common data elements at all. In practice, one usually stores documents of the same structure within collections.(我们存储的文档拥有不同的字段,事实上所有的文档没有相同的数据元素,在实际运用中,我们常常把相同数据结构的文档存储到一个集合中)
  • Upon being inserted into the database, objects are assigned an object ID (if they do not already have one) in the field _id.(每条插入的文档对象会被自动用object ID来标识)
  • When you run the above example, your ObjectID values will be different.(每个文档对象的ObjectID是唯一的)

下面我们使用JavaScript脚本插入20条记录

Note that not all documents were shown - the shell limits the number to 20 when automatically iterating a cursor. Since we already had 2 documents in the collection, we only see the first 18 of the newly-inserted documents.(MongoDB不会显示所有查询的文档,只会显示20条数据,尽管我们还有2条数据在集合中,我们只看到了18条新插入的文档)

If we want to return the next set of results, there's the it shortcut. Continuing from the code above(如果我们要继续显示下一页数据,只需在脚本中输入”it”就会继续显示上述查询剩下的数据)

Technically, find() returns a cursor object. But in the cases above, we haven't assigned that cursor to a variable. So, the shell automatically iterates over the cursor, giving us an initial result set, and allowing us to continue iterating with the it command.(find()函数返回的是一个游标对象,上述操作中,我们没有把这个游标赋给一个变量。所以,系统会自动遍历这个游标对象,返回给我们一个初始集合,并且允许我们继续通过输入”it”命令继续遍历)

当然我们可以使用游标对象赋给一个变量,把所有的文档遍历

The above example shows cursor-style iteration. The hasNext() function tells if there are any more documents to return, and the next() function returns the next document. We also used the built-in printjson() method to render the document in a pretty JSON-style format.

上述例子展示了游标方式的遍历,hasNext()函数告诉系统是否还有文档返回,next()函数则返回下一条文档。printjson()方法使文档对象以JSON格式显示。

When working in the JavaScript shell, we can also use the functional features of the language, and just call forEach on the cursor. Repeating the example above, but using forEach() directly on the cursor rather than the while loop:

当使用JavaScript脚本的时候,我们还可以使用该语言的一些功能特性,只需要在指针后面调用forEach(),里面回调了一个printjson方法,同样可以达到相同的效果,只不过使用游标比while循环更加直接

In the case of a forEach() we must define a function that is called for each document in the cursor.

使用forEache(),我们必须定义一个函数为游标中每个文档对象回调。

 

我们还可以把游标当做数组来使用

When using a cursor this way, note that all values up to the highest accessed (cursor[4] above) are loaded into RAM at the same time. This is inappropriate for large result sets, as you will run out of memory. Cursors should be used as an iterator with any query which returns a large number of elements.(使用上述方法,cursor[4]之前的数据会全部被载入到RAM中,对于大量的结果的集合来说是非常不合适的,有可能会发生内存溢出。指针应该被用作来遍历一个返回的大量元素的查询)。

In addition to array-style access to a cursor, you may also convert the cursor to a true array:

为了当做数组来使用,我们应该把游标转换为一个真正的数组

array features are specific to mongo - The Interactive Shell, and not offered by all drivers.

数组特性是在mongo交互式脚本中定义的,并不是所有的驱动都提供这个特性

 

查询

SQL select * from things;

SQL :SELECT * FROM things WHERE name="mongo"

SELECT j FROM things WHERE x=4

限制查询的数目(3-6)

查看函数的源码

我们在查看

Tojson

我们可以看到全是JS脚本。

更多JS API可以查看http://api.mongodb.org/js/

 

 

 

原文地址:https://www.cnblogs.com/ArtsCrafts/p/MongoDB.html